HttpURLConnection ----------------------------------------------- InetSocketAddress addr = new InetSocketAddress("127.0.0.1",1080); Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // http 代理 urlConnection = (HttpURLConnection) url.openConnection(proxy); ------------------------------------------------
HttpClient ------------------------------------------------ HttpHost proxy = new HttpHost("127.0.0.1", 1080); // 超时设置 // ConnectionRequestTimeout,从连接池获取连接的超时时间 // ConnectTimeout,连接服务器的超时时间 // SocketTimeout,传输数据的超时时间,是两个数据包之间的间隔时间,并非整体传输时间。如果一直有数据传输,不会触发此异常。 RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(30000).setConnectTimeout(30000) .setSocketTimeout(300000).setProxy(proxy).build(); httpPost.setConfig(requestConfig);
|