当我通过套接字发送正常的 HTTP 请求时,服务器没有以 OK 响应进行响应。我从 Firefox 复制了 HTTP 标头。这是代码:
Socket s = new Socket(InetAddress.getByName("stackoverflow.com"), 80);
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.print("GET / HTTP/1.1");
pw.print("Host: stackoverflow.com");
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String t;
while((t = br.readLine()) != null) System.out.println(t);
br.close();
但是,这是我收到的回复:
HTTP/1.0 408 Request Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>408 Request Time-out</h1>
Your browser didn't send a complete request in time.
</body></html>
我知道我可以通过使用来做到这一点URL.openStream()
,但是为什么我手动发送HTTP请求时服务器无法识别?