Home  >  Q&A  >  body text

How to change the http version number used when Java sends an http request?

How to change the http version number used when Java sends an http request? How do I manually implement using http 1.1 or http 1.0?

typechotypecho2686 days ago930

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-06-12 09:22:48

    Socket s = new Socket(InetAddress.getByName("stackoverflow.com"), 80);
    PrintWriter pw = new PrintWriter(s.getOutputStream());
    pw.println("GET / HTTP/1.1");
    pw.println("Host: caiyongji.com");
    pw.println("");
    pw.flush();
    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    String t;
    while((t = br.readLine()) != null) System.out.println(t);
    br.close();

    reply
    0
  • 怪我咯

    怪我咯2017-06-12 09:22:48

    It is not possible to use the HttpURLConnection that comes with Java. For details, please refer to:
    http://bugs.java.com/bugdatab...

    You can consider using Socket to send simple HTTP requests yourself, or use a third-party library. Take Apache HttpClient as an example:

    HttpClient client = new DefaultHttpClient(); 
    client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);

    reply
    0
  • Cancelreply