java代码中设置代理的方法

java代码中设置代理的方法:
1 使用URLConnection时

InetSocketAddress addr = null;
addr = new InetSocketAddress(ip,port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
URLConnection uc = urlObject.openConnection(proxy);

2使用httpclient时

//设置代理访问和超时处理
HttpHost proxy = new HttpHost(ip, Integer.parseInt(port));
RequestConfig config = RequestConfig.custom().setProxy(proxy).setConnectTimeout(3000). setSocketTimeout(3000).build();
// 根据地址获取请求
HttpGet request = new HttpGet(urlNameString);//这里发送get请求
request.setConfig(config);
// 获取当前客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 通过请求对象获取响应对象
System.out.println(request.getConfig());
HttpResponse response = httpClient.execute(request);

3 设置System相关的属性

//设置代理
System.setProperty("http.maxRedirects", "50");
System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("http.proxyHost", ip);
System.getProperties().setProperty("http.proxyPort", port);

这是一条华丽的分割线,对于HashMap的一些笔记

多线程下的引用关系

You are not suitable for the pair of laughing eyes frown
你那双爱笑的眼睛不适合皱眉

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容