报的异常为:
java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)GBK
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.test.Demo.main(Demo.java:27)
代码为:
package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
public class Demo {
public static void main(String[] args) throws IOException {
// 方法一 最常用的 PING 方法
System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("http.proxyHost", "10.6.0.200");
System.getProperties().setProperty("http.proxyPort", "808");
URL url = new URL("http://www.baidu.com");
HttpURLConnection httpCton = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpCton.setRequestMethod("GET");
httpCton.setRequestProperty(
"User-agent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36");
try {
InputStream in = httpCton.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in,
"UTF-8"));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Charset.defaultCharset().name());// 看默认Charset是什么
}
}
是通过一个代理服务器去访问百度的程序。永远报这个错。请大神指点迷津。
这里无论是使用IE的代理服务器,还是QQ,还是其它炒股软件,设置HTTP代理后,都正常。
公司使用的代理服务器是ccproxy。
------解决方案--------------------