HttpClient使用代理

代理服务器是客户端和Internet之间的中间服务器。代理服务器提供以下基本功能 -

  • 防火墙和网络数据过滤
  • 网络连接共享
  • 数据缓存

使用HttpClient库,可以使用代理发送HTTP请求。按照下面给出的步骤 -

第1步 - 创建一个HttpHost目标

通过将表示代理主机名称的字符串参数(从中需要发送请求)传递给其构造函数来实例化org.apache.http包的HttpHost类。

//Creating an HttpHost object for proxy
HttpHost proxyHost = new HttpHost("localhost");

Java

以同样的方式,创建另一个HttpHost对象来表示需要向其发送请求的目标主机。

//Creating an HttpHost object for target
HttpHost targetHost = new HttpHost("google.com");

Java

第2步 - 创建一个HttpRoutePlanner对象

HttpRoutePlanner接口计算到指定主机的路由,通过实例化DefaultProxyRoutePlanner类(此接口的实现)来创建此接口的对象。作为其构造函数的参数,传递上面创建的代理主机 -

//creating a RoutePlanner object
HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyhost);

Java

第3步 - 将路线规划器设置为客户端构建器
使用HttpClients类的custom()方法,创建一个HttpClientBuilder对象,并使用setRoutePlanner()方法为此对象设置上面创建的路径规划器。

//Setting the route planner to the HttpClientBuilder object
HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder = clientBuilder.setRoutePlanner(routePlanner);

Java

第4步 - 构建CloseableHttpClient对象
通过调用build()方法构建CloseableHttpClient对象。

//Building a CloseableHttpClient
CloseableHttpClient httpClient = clientBuilder.build();

Java

第5步 - 创建一个HttpGetobject
通过实例化HttpGet类来创建HTTP GET请求。

//Creating an HttpGet object
HttpGet httpGet = new HttpGet("/");

Java

第6步 - 执行请求
execute()方法的一个变体接受HttpHostHttpRequest对象并执行请求。使用此方法执行请求 -

//Executing the Get request
HttpResponse httpResponse = httpclient.execute(targetHost, httpGet);

Java

示例

以下示例演示了如何通过代理向服务器发送HTTP请求。在此示例中,通过localhost向google.com发送HTTP GET请求。打印了响应的标题和响应的正文。

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.util.EntityUtils;

public class RequestViaProxyExample {

   public static void main(String args[]) throws Exception{

      //Creating an HttpHost object for proxy
      HttpHost proxyhost = new HttpHost("localhost");

      //Creating an HttpHost object for target
      HttpHost targethost = new HttpHost("google.com");

      //creating a RoutePlanner object
      HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyhost);

      //Setting the route planner to the HttpClientBuilder object
      HttpClientBuilder clientBuilder = HttpClients.custom();
      clientBuilder = clientBuilder.setRoutePlanner(routePlanner);

      //Building a CloseableHttpClient
      CloseableHttpClient httpclient = clientBuilder.build();

      //Creating an HttpGet object
      HttpGet httpget = new HttpGet("/");

      //Executing the Get request
      HttpResponse httpresponse = httpclient.execute(targethost, httpget);

      //Printing the status line
      System.out.println(httpresponse.getStatusLine());

      //Printing all the headers of the response
      Header[] headers = httpresponse.getAllHeaders();

      for (int i = 0; i < headers.length; i++) {
         System.out.println(headers[i]);
      }

      //Printing the body of the response
      HttpEntity entity = httpresponse.getEntity();

      if (entity != null) {
         System.out.println(EntityUtils.toString(entity));
      }
   }
}

Java

执行上面示例代码,得到以下结果:

HTTP/1.1 200 OK
Date: Sun, 23 Dec 2018 10:21:47 GMT
Server: Apache/2.4.9 (Win64) PHP/5.5.13
Last-Modified: Tue, 24 Jun 2014 10:46:24 GMT
ETag: "2e-4fc92abc3c000"
Accept-Ranges: bytes
Content-Length: 46
Content-Type: text/html
<html><body><h1>It works!</h1></body></html>

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Apache HttpClient是一个非常流行的Java HTTP客户端库,它可以用来发送HTTP请求并处理响应。如果你需要使用代理服务器来发送HTTP请求,那么你可以使用HttpClient来获取代理列表。下面是一个使用HttpClient获取代理列表的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; public class ProxyUtil { public static List<HttpHost> getProxyList() throws Exception { List<HttpHost> proxyList = new ArrayList<>(); URI uri = new URIBuilder().setScheme("http").setHost("www.xicidaili.com").setPath("/nn/").setParameter("wt", "1").build(); HttpGet request = new HttpGet(uri); HttpClient client = HttpClientBuilder.create().build(); HttpResponse response = client.execute(request); String html = EntityUtils.toString(response.getEntity()); Document doc = Jsoup.parse(html); Elements elements = doc.select("table#ip_list tr"); for (int i = 1; i < elements.size(); i++) { String ip = elements.get(i).select("td:eq(1)").text(); String port = elements.get(i).select("td:eq(2)").text(); HttpHost proxy = new HttpHost(ip, Integer.parseInt(port)); proxyList.add(proxy); } return proxyList; } public static void main(String[] args) { try { List<HttpHost> proxyList = getProxyList(); for (HttpHost proxy : proxyList) { System.out.println(proxy); } } catch (Exception e) { e.printStackTrace(); } } } ``` 这个示例代码使用了一个免费的代理IP网站(www.xicidaili.com)来获取代理列表,然后解析HTML文档提取出代理IP和端口号,最后将它们封装成HttpHost对象并添加到代理列表中。你可以修改这个示例代码来适应其他的代理IP网站。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧浩海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值