HttpClient(4.3.5) - HTTP Header

An HTTP message can contain a number of headers describing properties of the message such as the content length, content type and so on. HttpClient provides methods to retrieve, add, remove and enumerate headers.

HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
Header h1 = response.getFirstHeader("Set-Cookie");
System.out.println(h1);
Header h2 = response.getLastHeader("Set-Cookie");
System.out.println(h2);
Header[] hs = response.getHeaders("Set-Cookie");
System.out.println(hs.length);

stdout >

Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
2

The most efficient way to obtain all headers of a given type is by using the HeaderIterator interface.

HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\"");

HeaderIterator it = response.headerIterator("Set-Cookie");
while (it.hasNext()) {
    System.out.println(it.next());
}

stdout >

Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path="/", c3=c; domain="localhost"

It also provides convenience methods to parse HTTP messages into individual header elements.

HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
response.addHeader("Set-Cookie", "c1=a; path=/; domain=localhost");
response.addHeader("Set-Cookie", "c2=b; path=\"/\", c3=c; domain=\"localhost\"");

HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("Set-Cookie"));
while (it.hasNext()) {
    HeaderElement elem = it.nextElement();
    System.out.println(elem.getName() + " = " + elem.getValue());
    NameValuePair[] params = elem.getParameters();
    for (int i = 0; i < params.length; i++) {
        System.out.println(" " + params[i]);
    }
}

stdout >

c1 = a
path=/
domain=localhost
c2 = b
path=/
c3 = c
domain=localhost
时间: 2024-11-05 11:57:51

HttpClient(4.3.5) - HTTP Header的相关文章

httpClient如何接收格式错误的响应头部信息

Exception in thread "main" org.apache.commons.httpclient.ProtocolException: Unable to parse header: share memory not exist, need create new share memory! at org.apache.commons.httpclient.HttpParser.parseHeaders(HttpParser.java:202) at org.apache

C#: Create a WebRequest with HTTPClient

http://www.cnblogs.com/shanyou/archive/2012/03/21/2410739.html http://msdn.microsoft.com/zh-cn/library/system.net.http.httpclient.aspx http://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee using System.Net.Http; public static string GetH

网络编程(一)

get 方法实例: String url="http://202.194.14.215/iactivity/mobile/msg.php?act=1"; String tixingfanhuizhi=null; DefaultHttpClient client=new DefaultHttpClient();//新建client HttpGet get=new HttpGet(url); //新建httpget HttpResponse response=null; get.setHe

Android开发-自动更新

为车机写apk,先实现版本的自动更新. 1.不能再主线程中调用会阻塞ui的功能,需要使用异步方式调用网络,引入Android Async Http框架,需要两个包:android-async-http-1.4.9.jar.httpclient-4.4.1.2.jar,否则会出现httpclient中找不到Header[]类型. 2.在AndroidManifest.xml中加入网络访问权限(<uses-permission android:name="android.permission.

创建http客户端,请求其他服务接口(GET/POST)

service1拥有接口 : GET      user/{user_id} POST    user/add service2调用service1的接口获取数据 1.创建客户端 //定义客户端结构体 type SimpleClient struct { Client HTTPRequest *http.Request //用于返回执行时的request,直接设值无效.引用时注意做非空检查. HTTPResponse *http.Response //用于返回执行结果.引用时注意做非空检查. }

httpclient: Content-Length header already present问题

现象:用httpclient发送http请求时,客户端返回: org.apache.http.client.ClientProtocolException at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754

HttpClient取得自定义的状态码302,并获取Header中的参数Location

1.导入如下两个包: 下载地址:http://files.cnblogs.com/files/zhougaojun/httpclient_lib.zip 2.接收Google返回的302状态码,并获得Location参数,如下是代码关键部分 DefaultHttpClient httpclient = new DefaultHttpClient(); String location = null; int responseCode = 0; try { final HttpGet request

轻松把玩HttpClient之封装HttpClient工具类(三),插件式配置Header

上篇文章介绍了插件式配置HttpClient,本文将介绍插件式配置Header. 为什么要配置header在前面已经提到了,还里再简单说一下,要使用HttpClient模拟请求,去访问各种接口或者网站资源,都有可能有各种限制,比如说java客户端模拟访问csdn博客,就必须设置User-Agent,否则就报错了.还有各种其他情况,必须的设置一些特定的Header,才能请求成功,或者才能不出问题. 好了就说这么多,本次还是采用构造者模式的级联调用方式,来完成该工具类.在该工具类中,为所有常用的Ht

后台发送请求,HttpClient的post,get各种请求,带header的请求

HttpClient依赖jar包: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> 写一个发送请求的工具类HttpUtil package com.bs.utils; import java.io