Wie ändere ich die verwendete http-Versionsnummer, wenn Java eine http-Anfrage sendet? Wie kann ich manuell mit http 1.1 oder http 1.0 implementieren?
Following the voice in heart.
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();
用 Java 自带的 HttpURLConnection 是做不到的,详情参考:http://bugs.java.com/bugdatab...
可以考虑自己用 Socket 发送简单的 HTTP 请求,或者用第三方库。以 Apache HttpClient 为例:
HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
用 Java 自带的 HttpURLConnection 是做不到的,详情参考:
http://bugs.java.com/bugdatab...
可以考虑自己用 Socket 发送简单的 HTTP 请求,或者用第三方库。以 Apache HttpClient 为例: