为什么Jsoup设置***IP后连接失败
不使用proxy时运行十分稳定,一旦在Connection对象中设置proxy后就报错:connect time out。请求各位大神指导一二
主方法:
public class ProxyIpUtilTest { public static void main(String[] args) { int code = ProxyIpUtil.checkProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("180.118.134.123", 9000)), "http://www.baidu.com"); System.out.println(code); } }
ProxyIpUtil.checkProxy():
public static int checkProxy(Proxy proxy, String validSite){ try { URL url = new URL(validSite!=null?validSite:XxlCrawlerConf.SITE_BAIDU); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(proxy); httpURLConnection.setRequestProperty("User-Agent", XxlCrawlerConf.USER_AGENT_CHROME); httpURLConnection.setConnectTimeout(XxlCrawlerConf.TIMEOUT_MILLIS_DEFAULT); httpURLConnection.setReadTimeout(XxlCrawlerConf.TIMEOUT_MILLIS_DEFAULT); httpURLConnection.connect(); int statusCode = httpURLConnection.getResponseCode(); /*InputStream inputStream = httpURLConnection.getInputStream(); String content = IOUtil.toString(inputStream, null); if(content.indexOf("百度") == -1){ logger.info(content); return -1; }*/ return statusCode; } catch (IOException e) { logger.error(e.getMessage(), e); return -2; } }
报错:
java.net.SocketTimeoutException: connect timed out at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.Socket.connect(Socket.java:589) at sun.net.NetworkClient.doConnect(NetworkClient.java:175) at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) at sun.net.www.http.HttpClient$1.run(HttpClient.java:515) at sun.net.www.http.HttpClient$1.run(HttpClient.java:513) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.http.HttpClient.privilegedOpenServer(HttpClient.java:512) at sun.net.www.http.HttpClient.openServer(HttpClient.java:553) at sun.net.www.http.HttpClient.<init>(HttpClient.java:242) at sun.net.www.http.HttpClient.New(HttpClient.java:339) at sun.net.www.http.HttpClient.New(HttpClient.java:357) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1220) at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1199) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:984) at com.xuxueli.crawler.util.ProxyIpUtil.checkProxy(ProxyIpUtil.java:36) at com.xuxueli.crawler.test.util.ProxyIpUtilTest.main(ProxyIpUtilTest.java:30) -2#Java#