美文网首页
java代码中设置代理的方法

java代码中设置代理的方法

作者: 刘同学lwq | 来源:发表于2018-01-24 13:29 被阅读0次
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
你那双爱笑的眼睛不适合皱眉

相关文章

网友评论

      本文标题:java代码中设置代理的方法

      本文链接:https://www.haomeiwen.com/subject/wwcuaxtx.html