简体   繁体   English

Java-apache http客户端使用示例,显示使用cookie和从HTTPResponse对象中提取响应

[英]Java- apache http client- usage examples showing use of cookies and extracting response from HTTPResponse object

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

(1) How to use Cookies with Apache HTTP client, different options available for usage of cookies (1)如何将Cookie与Apache HTTP客户端一起使用,可以使用不同的cookie选项

(2) Extracting charset, mimetype, response headers (as KeyValuePair) and budy (as byte[]) when the response is available in HTTPResponse object. (2)当HTTPResponse对象中的响应可用时,提取字符集,mimetype,响应头(作为KeyValuePair)和budy(作为byte [])。

1)AS for cookies,see that exapmle: 1)用于cookie的AS,请参阅exapmle:

httpcomponents-client-4.1.3\\examples\\org\\apache\\http\\examples\\client\\ClientCustomContext.java httpcomponents-客户4.1.3 \\例子\\组织\\阿帕奇\\ HTTP \\例子\\客户\\ 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: 2)您可以从响应中获得所需的一切:

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 . 只需阅读httpcomponents-client-4.1.3-bin.zip的httpcomponents-client-4.1.3 \\ examples \\ org \\ apache \\ http \\ examples \\ client中的示例,该文件从其网站下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM