这两天我通过一整个代理ip池多线程去访问某个网页,但是在运行到某几个代理的时候线程会无限卡死。
public static String getHtml1(String Url, String proxyIp, int proxyPort, int timeOut) throws IOException {
InetSocketAddress socketAddress = new InetSocketAddress(
InetAddress.getByName(proxyIp), proxyPort);
Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
URL url = new URL(Url);
URLConnection conn = url.openConnection(proxy);
conn.setConnectTimeout(timeOut);
InputStream input = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = "";
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
bufferedReader.close();
return stringBuffer.toString();
}
经过一天的测试发现,主要原因是从在我只是设置了连接时常。并没有设置从服务器上面读取流的超时设置。因为只需要加上一行代码就可以解决问题。
conn.setReadTimeout(1000);