Java 8 抛出异常,而 Java 6 在执行 GET 请求时不抛出异常
Java 8 throws exception where Java 6 does not when executing GET request
我正在使用简单的代码来执行获取请求和加载页面数据。相关代码在这里:
public class HttpTest {
public static void main(String[] args) throws IOException {
URL url = new URL(
"http://" + args[0] + "/send?pts=900000000&place=1");
URLConnection conn = url.openConnection();
conn.setReadTimeout(3000);
conn.setConnectTimeout(3000);
StringBuilder result = new StringBuilder();
InputStream in = conn.getInputStream();
int c;
while ((c = in.read()) != -1) {
result.append((char) c);
}
String response = result.toString();
System.out.println(response);
}
}
当我用 Java 6 执行代码时,一切正常,我的代码打印响应。但是当我用 Java 8 执行它时,我在打开流时出错:
Exception in thread "main" java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1553)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at HttpTest.main(HttpTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
我尝试了不同的方法并使用了一些库,但总是使用 Java 8 我收到错误。
当 curl-ing 那个 URL 或用邮递员测试它时,我可以看到响应不包含任何 header 字段和状态,但我无法更改提供该响应的服务,所以不知何故我必须告诉 Java 8 冷静一下缺少代码 200 和 headers 并告诉我 body 中有什么。怎么样?
除非明确指定,否则您不能期望 http
客户端实现具有容错能力。因此,如果您知道服务器实际上不是 http
服务器(严格来说),您可以简单地实现一个手动套接字访问,模仿服务器理解的 http
协议。就这么简单
String host = args[0];
try(Socket s=new Socket(host, 80)) {
Writer w=new OutputStreamWriter(s.getOutputStream(), StandardCharsets.US_ASCII);
w.write("GET http://"+host+"/send?pts=900000000&place=1 HTTP/1.0\r\n\r\n");
w.flush();
// rest taken from your original code, what you are basically doing
// is interpreting the received data like being ISO_8851_1 encoded
// you might have to strip off the remains of the return header, if there is one
StringBuilder result = new StringBuilder();
InputStream in = s.getInputStream();
int c;
while ((c = in.read()) != -1) {
result.append((char) c);
}
String response = result.toString();
System.out.println(response);
}
我正在使用简单的代码来执行获取请求和加载页面数据。相关代码在这里:
public class HttpTest {
public static void main(String[] args) throws IOException {
URL url = new URL(
"http://" + args[0] + "/send?pts=900000000&place=1");
URLConnection conn = url.openConnection();
conn.setReadTimeout(3000);
conn.setConnectTimeout(3000);
StringBuilder result = new StringBuilder();
InputStream in = conn.getInputStream();
int c;
while ((c = in.read()) != -1) {
result.append((char) c);
}
String response = result.toString();
System.out.println(response);
}
}
当我用 Java 6 执行代码时,一切正常,我的代码打印响应。但是当我用 Java 8 执行它时,我在打开流时出错:
Exception in thread "main" java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1553)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at HttpTest.main(HttpTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
我尝试了不同的方法并使用了一些库,但总是使用 Java 8 我收到错误。 当 curl-ing 那个 URL 或用邮递员测试它时,我可以看到响应不包含任何 header 字段和状态,但我无法更改提供该响应的服务,所以不知何故我必须告诉 Java 8 冷静一下缺少代码 200 和 headers 并告诉我 body 中有什么。怎么样?
除非明确指定,否则您不能期望 http
客户端实现具有容错能力。因此,如果您知道服务器实际上不是 http
服务器(严格来说),您可以简单地实现一个手动套接字访问,模仿服务器理解的 http
协议。就这么简单
String host = args[0];
try(Socket s=new Socket(host, 80)) {
Writer w=new OutputStreamWriter(s.getOutputStream(), StandardCharsets.US_ASCII);
w.write("GET http://"+host+"/send?pts=900000000&place=1 HTTP/1.0\r\n\r\n");
w.flush();
// rest taken from your original code, what you are basically doing
// is interpreting the received data like being ISO_8851_1 encoded
// you might have to strip off the remains of the return header, if there is one
StringBuilder result = new StringBuilder();
InputStream in = s.getInputStream();
int c;
while ((c = in.read()) != -1) {
result.append((char) c);
}
String response = result.toString();
System.out.println(response);
}