leftso 986 0 2018-04-06 21:22:28

logo-cover-HttpClient 4 发送自定义的Cookie

1.概述

本教程将重点介绍如何使用Apache HttpClient 4发送自定义Cookie

 

2.在HttpClient上配置Cookie管理

2.1。4.3之后的HttpClient

在较新的HttpClient 4.3中,我们将利用负责构建和配置客户端的流利构建器API。

首先,我们需要创建一个Cookie存储并在商店中设置我们的示例Cookie:

BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);

然后,我们可以在HttpClient上设置这个cookie存储并发送请求:
 

@Test
public void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly() 
  throws ClientProtocolException, IOException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
    cookie.setDomain(".github.com");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
 
    final HttpGet request = new HttpGet("http://www.github.com");
 
    response = client.execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

一个非常重要的元素是被设置上的Cookie - 没有设置正确的域,客户端将不会发送该cookie,在所有!

2.2。4.3之前的HttpClient

使用旧版本的HttpClient(4.3之前) - cookie存储直接在HttpClient上设置:

@Test
public void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect() 
  throws ClientProtocolException, IOException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
    cookie.setDomain(".github.com");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    DefaultHttpClient client = new DefaultHttpClient();
    client.setCookieStore(cookieStore);
 
    HttpGet request = new HttpGet("http://www.github.com");
 
    response = client.execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

除了客户端的构建方式之外,与前面的示例没有其他区别。

3.在请求中设置Cookie

如果在整个HttpClient上设置cookie不是一个选项,我们可以单独配置cookie的请求:

@Test
public void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly() 
  throws ClientProtocolException, IOException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
    cookie.setDomain(".github.com");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    instance = HttpClientBuilder.create().build();
 
    HttpGet request = new HttpGet("http://www.github.com");
 
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    // localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
    response = instance.execute(request, localContext);
 
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

4.在低层请求中设置Cookie

在HTTP请求中设置Cookie的低级替代方案将会将其设置为原始标题:

@Test
public void whenSettingCookiesOnARequest_thenCorrect() 
  throws ClientProtocolException, IOException {
    instance = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet("http://www.github.com");
    request.setHeader("Cookie", "JSESSIONID=1234");
 
    response = instance.execute(request);
 
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

与使用内置cookie支持相比,这当然更容易出错 - 例如,注意我们在这种情况下不再设置域 - 这是不正确的。

5.总结

本文说明了如何使用HttpClient发送自定义的,用户控制的Cookie

请注意,这与让HttpClient处理由服务器设置的cookie不同 - 但是它却是在低级别手动控制客户端。