首页 文章

无法在java中读取http post请求的表单数据

提问于
浏览
0

程序只是读取表单并停止 .

//...

Socket client = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

System.out.println("Request:");

while ((line = in.readLine()) != null)
    if (line.length() > 0)
    System.out.println("\t" + line);
        else break;

in.close();
client.close();

当我尝试在浏览器上提交某个表单时:

Waiting for request...
Connection accepted.
Request:
    POST /form.php HTTP/1.1
    Host: x-x-x-x
    Connection: keep-alive
    Content-Length: 31
    Cache-Control: max-age=0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Origin: http://neviat.us.to
    User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
    Content-Type: application/x-www-form-urlencoded
    Referer: http://x-x-x-x
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Connection closed.

我得到了 Content-Length ,但内容本身并没有 . ¬-我认为那可能是 else break; ,所以我把它取下来再试一次:

Waiting for request...
Connection accepted.
Request:
    POST /buy.php HTTP/1.1
    Host: x-x-x-x
    Connection: keep-alive
    Content-Length: 31
    Cache-Control: max-age=0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    Origin: http://neviat.us.to
    User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
    Content-Type: application/x-www-form-urlencoded
    Referer: http://x-x-x-x
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4

    // stop here, waiting for stream...

但内容也不存在 .

我怎么才能得到它?

1 回答

  • 0

    更改:

    if (line.length() > 0)
    

    if (line.length() > -1)
    

    0长度的行是完全有效的

相关问题