程序刚读完表格并停止。
//...
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...
但内容也不存在。
我怎样才能得到它?
答案 0 :(得分:0)
变化:
if (line.length() > 0)
到
if (line.length() > -1)
0长度的行是完全有效的
答案 1 :(得分:0)
更改代码:
// ...
Socket client = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("Request:");
while ((line = in.readLine()) != -1)
if (line.length() > 0)
System.out.println("\t" + line);
else break;
in.close();
client.close();