简体   繁体   English

Java中的原始HTTP1.1请求实现未终止

[英]Raw HTTP1.1 request implementaion in Java not getting terminated

I have implemented httpClient by using the code from the below link. 我已经通过使用以下链接中的代码实现了httpClient。 I am able to get the html repose and program is getting terminated when the HTTP request is of version 1.0. 当HTTP请求的版本为1.0时,我能够获得html休止符并终止程序。

https://www3.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html https://www3.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html

If I change the version to 1.1 , the reponse is printed,but program is not getting terminated even after that. 如果我将版本更改为1.1,则会打印响应,但即使在此之后程序也不会终止。 Please mention the changes and suggest the changes. 请提及更改并提出更改建议。

import java.net.*;
import java.io.*;

public class HttpClient {
   public static void main(String[] args) throws IOException {
      // The host and port to be connected.
      String host = "www.google.com";
      int port = 80;
      // Create a TCP socket and connect to the host:port.
      Socket socket = new Socket(host, port);
      // Create the input and output streams for the network socket.
      BufferedReader in
         = new BufferedReader(
              new InputStreamReader(socket.getInputStream()));
      PrintWriter out
         = new PrintWriter(socket.getOutputStream(), true);
      // Send request to the HTTP server.
      out.println("GET /index.html HTTP/1.1");
      out.println();   // blank line separating header & body
      out.flush();
      // Read the response and display on console.
      String line;
      // readLine() returns null if server close the network socket.
      while((line = in.readLine()) != null) {
         System.out.println(line);
      }
      // Close the I/O streams.
      in.close();
      out.close();
   }
}

HTTP keepalive is on by default in 1.1. 默认情况下,HTTP keepalive在1.1中处于启用状态。 So the response isn't terminated by end of stream: it is terminated by the Content-length or the chunking if used. 因此,响应不会在流的末尾终止:而是通过Content-length或分块(如果使用)终止。

See RFC 2616. If you're implementing HTTP you need to know it all. 请参阅RFC2616。如果要实现HTTP,则需要全部了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM