出現java.net.SocketTimeoutException: Read timed out的問題
最後定位到如下地方:
httpURL = new URL(url);
System.out.println("-----------url:"+url+"--------------");
connection = (HttpURLConnection) httpURL.openConnection();
System.out.println("-----------httpURL.openConnection()--------------");
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setRequestMethod("GET");
connection.setConnectTimeout(2000);
connection.setReadTimeout(2000);
System.out.println("-----------connection.setReadTimeout(30000)--------------");
connection.connect();
System.out.println("-----------connection.connect()--------------");
cin = connection.getInputStream();
System.out.println("-----------connection.getInputStream():"+cin.toString()+"--------------");
BufferedInputStream bci = new BufferedInputStream(cin);
System.out.println("-----------new BufferedInputStream(cin)--------------");
SAXReader reader = new SAXReader();
doc = reader.read(bci);
System.out.println("-----------doc = reader.read(bci);:"+doc.toString()+"--------------");
一開始並沒有任何提示,這些提示都是後來加上去的
查了很多,大家給的提議要麼就是提高機子的效能,要麼就是將timeout的時間延長
提高機子的效能肯定不行了,於是我加大了timeout的時間,可是開始不行!!!!
鬱悶,又看了看,網上似乎提到Proxy 伺服器好像跟這個有關,正好這時看見一個兄台提示,如是對代碼做了以下修改
URL httpURL = null;
HttpURLConnection connection = null;
Document doc = null;
InputStream cin = null;
try {
Properties prop = System.getProperties();
System.getProperties().put("proxySet","true");
// 設定http訪問要使用的Proxy 伺服器的地址
prop.setProperty("http.proxyHost","172.17.0.2");
// 設定http訪問要使用的Proxy 伺服器的連接埠
prop.setProperty("http.proxyPort","8080");
httpURL = new URL(url);
System.out.println("-----------url:"+url+"--------------");
connection = (HttpURLConnection) httpURL.openConnection();
System.out.println("-----------httpURL.openConnection()--------------");
String authentication = "username:password"; // 使用者名稱密碼
String encodedLogin = new BASE64Encoder().encode(authentication.getBytes()); // 編碼
connection.setRequestProperty("Proxy-Authorization", " Basic " + encodedLogin); // 授權
System.out.println("-----------connection.setRequestProperty:"+encodedLogin+"--------------");
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
connection.setReadTimeout(30000);
System.out.println("-----------connection.setReadTimeout(30000)--------------");
connection.connect();
System.out.println("-----------connection.connect()--------------");
cin = connection.getInputStream();
System.out.println("-----------connection.getInputStream():"+cin.toString()+"--------------");
BufferedInputStream bci = new BufferedInputStream(cin);
System.out.println("-----------new BufferedInputStream(cin)--------------");
SAXReader reader = new SAXReader();
doc = reader.read(bci);
System.out.println("-----------doc = reader.read(bci);:"+doc.toString()+"--------------");
} catch (Exception e) {
e.printStackTrace();
doc = null;
} finally {
if (cin != null) {
try {
cin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
connection.disconnect();
}
}
標紅的地方為修改的地方,就是為請求添加了個Proxy 伺服器