Apache Httpclient 简明教程
Apache HttpClient - Using Proxy
代理服务器是介于客户端和互联网之间的中间服务器。代理服务器提供的基本功能包括 -
-
防火墙和网络数据过滤
-
Network connection sharing
-
Data caching
使用 HttpClient 库,您可以通过代理发送 HTTP 请求。请按照以下步骤操作 -
Step 1 - Create a HttpHost object
通过传递表示代理主机名称的字符串参数(你需要从中发送请求),来实例化`@ {s1}`包中的`@ {s0}`类。
//Creating an HttpHost object for proxy
HttpHost proxyHost = new HttpHost("localhost");
以同样的方法,创建另一个`HttpHost`对象来表示需要发送请求的目标主机。
//Creating an HttpHost object for target
HttpHost targetHost = new HttpHost("google.com");
Step 2 - Create an HttpRoutePlanner object
@ {s2}`接口计算到指定主机的一条路由。通过实例化
@ {s3}`类,该接口的实现来创建一个此接口的对象。作为其构造函数的参数,传递上面创建的代理主机 -
//creating a RoutePlanner object
HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyhost);
Step 3 - Set the route planner to a client builder
使用`@ {s5}`类的`@ {s4}`方法,创建一个`@ {s6}`对象,并使用`@ {s7}`方法将上面创建的路由计划程序设置到此对象。
//Setting the route planner to the HttpClientBuilder object
HttpClientBuilder clientBuilder = HttpClients.custom();
clientBuilder = clientBuilder.setRoutePlanner(routePlanner);
Step 4 - Build the CloseableHttpClient object
通过调用`@ {s9}`方法来构建`@ {s8}`对象。
//Building a CloseableHttpClient
CloseableHttpClient httpClient = clientBuilder.build();
Step 5 - Create a HttpGetobject
通过实例化`@ {s10}`类来创建一个HTTP GET请求。
//Creating an HttpGet object
HttpGet httpGet = new HttpGet("/");
Step 6 - Execute the request
@ {s11}`方法的一个变体接受
@ {s12}`和`@ {s13}`对象并执行该请求。使用这种方法执行请求 -
//Executing the Get request
HttpResponse httpResponse = httpclient.execute(targetHost, httpGet);
Example
以下示例演示了如何通过代理将HTTP请求发送到服务器。在本例中,我们正在通过本地主机向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));
}
}
}