通过套接字手动发送 HTTP 请求
2022-08-31 19:51:38
当我通过套接字发送正常的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>
我知道我可以使用 来执行此操作,但是为什么当我手动发送HTTP请求时,服务器无法识别它?URL.openStream()