文章将介绍在Apache HttpClient 4 中cookie的使用。
在新的HttpClient 4.3中。我们将充分利用开放的API来管理设置我们的客户端链接
首先,我们需要创造一个cookie store 并将我们的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();</p> <pre><code>final HttpGet request = new HttpGet("http://www.github.com"); response = client.execute(request); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); </code>
} 设置那个域名十分重要。不设置的话,客户端将不会发送cookie,相当于cookie绑定在.github.com上
在4.3之前的HttpClient 版本中,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);</p> <pre><code>HttpGet request = new HttpGet("http://www.github.com"); response = client.execute(request); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); </code>
} 除了client 的创建,其他地方没有区别;
要设置整个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();</p> <pre><code>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)); </code>
}
在Header上设置简单的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更容易出错,在我们没有设置域名的情况下,这样的做法就是错的。
本文演示了如何使用Httpclient自定义cookie发送请求