HttpClient系列-基础知识(二)

简述

今天开始介绍下基础知识二,主要内容是请求的中断、重定向。json

中断请求

如何使用Apache HttpClient 4取消HTTP请求。bash

这对于可能长时间运行的请求或大型下载文件尤为有用,不然这些请求将没必要要地消耗带宽和链接。app

停止GET请求

要停止正在进行的请求,客户端能够简单地使用:ui

request.abort();
复制代码

这将确保客户端没必要使用整个请求来释放链接:spa

@Test
public void test() 
  throws ClientProtocolException, IOException {
    HttpClient instance = HttpClients.custom().build();
    HttpGet request = new HttpGet(SAMPLE_URL);
    HttpResponse response = instance.execute(request);
 
    try {
        System.out.println(response.getStatusLine());
        request.abort();
    } finally {
        response.close();
    }
}
复制代码

禁止重定向

默认状况下,遵循HTTP规范,HttpClient将自动遵循重定向code

对于某些用例来讲,这多是彻底没问题的,但确定会出现不须要的用例 。如今咱们将看看如何更改默认行为并中止重定向。get

在HttpClient 4.3以前string

在旧版本的HttpClient(4.3以前)中,咱们能够配置客户端使用重定向执行的操做,以下所示:it

@Test
public void test() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient instance = new DefaultHttpClient();
 
    HttpParams params = new BasicHttpParams();
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, false);
    // HttpClientParams.setRedirecting(params, false); // alternative

    HttpGet httpGet = new HttpGet("http://localhost:8080");
    httpGet.setParams(params);
    CloseableHttpResponse response = instance.execute(httpGet);
 
    assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
复制代码

请注意可用于配置重定向行为的备用API,而不使用设置实际原始http.protocol.handle-redirects参数:io

HttpClientParams.setRedirecting(params, false);
复制代码

另请注意,若是禁用了后续重定向,咱们如今能够检查Http响应状态代码是否确实是301 Moved Permaned - 应该是这样。

在HttpClient 4.3以后

HttpClient 4.3引入了更清晰,更高级的API来构建和配置客户端:

@Test
public void test() 
  throws ClientProtocolException, IOException {
    HttpClient instance = HttpClientBuilder.create().disableRedirectHandling().build();
HttpResponse response = instance.execute(new HttpGet("http://localhost:8080"));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(301));

复制代码

PS:新API使用此重定向行为配置整个客户端 - 而不单单是单个请求。

自定义Hearder

根据要求设置Hearder(4.3及以上)

HttpClient 4.3引入了一种构建请求的新API--RequestBuilder。要设置Header,咱们将在构建器上使用setHeader方法:

HttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.get()
  .setUri(SAMPLE_URL)
  .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
  .build();
client.execute(request);
复制代码

根据要求设置Hearder(4.3以前)

在HttpClient 4.3以前的版本中,咱们能够在请求上使用简单的setHeader调用设置任何自定义标头:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(SAMPLE_URL);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
client.execute(request);
复制代码

咱们能够看到,咱们将Content-Type直接在请求上设置为自定义值--JSON。

在客户端上设置默认标头

咱们还能够将其配置为客户端自己的默认标头,而不是在每一个请求上设置标头:

Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
List<Header> headers = Lists.newArrayList(header);
HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();
client.execute(request);
复制代码

当全部请求的标头须要相同时(例如自定义应用程序标头),这很是有用。

小结

本文介绍了HttpClient中的中断请求,禁止跟踪HTTP重定向,自定义Header知识,但愿对你有所帮助。

相关文章
相关标签/搜索