Java-apache http客户端使用示例,显示使用cookie和从HTTPResponse对象中提取响应 [英] Java- apache http client- usage examples showing use of cookies and extracting response from HTTPResponse object

查看:1461
本文介绍了Java-apache http客户端使用示例,显示使用cookie和从HTTPResponse对象中提取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java web应用程序中使用apache http client(v4),我遇到以下情况,我需要简单的用法示例 -



(1)如何在Apache HTTP客户端使用Cookie,可以使用不同的cookie选项



(2)提取字符集,mimetype,响应头(作为KeyValuePair)和响应在HTTPResponse对象中可用的budy(如byte [])。

解决方案

1)用于cookie的AS,请参阅exapmle:



httpcomponents-client-4.1.3 \ examples.\\\\\\\\ >

主要代码:

  HttpClient httpclient = new DefaultHttpClient(); 
try {
//创建cookie存储的本地实例
CookieStore cookieStore = new BasicCookieStore();

//创建本地HTTP上下文
HttpContext localContext = new BasicHttpContext();
//将自定义cookie存储绑定到本地上下文
localContext.setAttribute(ClientContext.COOKIE_STORE,cookieStore);

HttpGet httpget = new HttpGet(http://www.google.com/);

System.out.println(执行请求+ httpget.getURI());

//将本地上下文作为参数传递
HttpResponse response = httpclient.execute(httpget,localContext);
} finally {
//当不再需要HttpClient实例时,
//关闭连接管理器以确保
//立即释放所有系统资源
。httpclient.getConnectionManager()关闭();
}

2)您可以从响应中获得所需的一切:

  HttpEntity entity = response.getEntity(); 
entity.getContent()

请阅读以下示例:
httpcomponents- client-4.1.3\examples\org\apache\http\examples\client
httpcomponents-client-4.1.3-bin.zip,从网站


I am working with apache http client (v4) in a java web app, and I am stuck in the following cases, for which I require simple usage examples--

(1) How to use Cookies with Apache HTTP client, different options available for usage of cookies

(2) Extracting charset, mimetype, response headers (as KeyValuePair) and budy (as byte[]) when the response is available in HTTPResponse object.

解决方案

1)AS for cookies,see that exapmle:

httpcomponents-client-4.1.3\examples\org\apache\http\examples\client\ClientCustomContext.java

main code:

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)You can get all you need from response and:

HttpEntity entity = response.getEntity();
entity.getContent()

Just read the examples in: httpcomponents-client-4.1.3\examples\org\apache\http\examples\client of httpcomponents-client-4.1.3-bin.zip which is downloaded from its website.

这篇关于Java-apache http客户端使用示例,显示使用cookie和从HTTPResponse对象中提取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆