我正在使用 java web 应用程序中的 apache http 客户端 (v4),并且遇到以下情况,我需要简单的使用示例——
(1) 如何在 Apache HTTP 客户端中使用 Cookie,不同的选项可用于使用 cookie
(2) 当响应在 HTTPResponse 对象中可用时,提取字符集、mimetype、响应头(作为 KeyValuePair)和伙伴(作为 byte[])。
我正在使用 java web 应用程序中的 apache http 客户端 (v4),并且遇到以下情况,我需要简单的使用示例——
(1) 如何在 Apache HTTP 客户端中使用 Cookie,不同的选项可用于使用 cookie
(2) 当响应在 HTTPResponse 对象中可用时,提取字符集、mimetype、响应头(作为 KeyValuePair)和伙伴(作为 byte[])。
1)对于 cookie,请参阅该示例:
httpcomponents-client-4.1.3\examples\org\apache\http\examples\client\ClientCustomContext.java
主要代码:
HttpClient httpclient = new DefaultHttpClient();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet("http://www.google.com/");
System.out.println("executing request " + httpget.getURI());
// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
2)您可以从响应中获得所需的一切,并且:
HttpEntity entity = response.getEntity();
entity.getContent()
只需阅读从其网站下载的 httpcomponents-client-4.1.3-bin.zip 的 httpcomponents-client-4.1.3\examples\org\apache\http\examples\client 中的示例。