HttpClient 响应头内容获取

HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet("http://www.baidu.com");
		HttpResponse httpResponse = httpclient.execute(httpGet);

		Header[] headerArray = httpResponse.getAllHeaders();
		for(Header header : headerArray)
		{
			System.out.println("--Header-----------------------------------------");
			System.out.println("----Key: " + header.getName());
			System.out.println("----RawValue: " + header.getValue());
			HeaderElement[] headerElementArray = header.getElements();
			for(HeaderElement headerElement : headerElementArray)
			{
				System.out.print("------Value: " + headerElement.getName());
				if(null != headerElement.getValue())
				{
					System.out.println("  <-|->  " + headerElement.getValue());
				}
				else
				{
					System.out.println();
				}
				NameValuePair[] nameValuePairArray = headerElement.getParameters();
				for(NameValuePair nameValuePair : nameValuePairArray)
				{
					System.out.println("------Parameter: " + nameValuePair.getName() + "  <-|->  " + nameValuePair.getValue());
				}
			}
		}

 

执行结果:

 

--Header-----------------------------------------
----Key: Date
----RawValue: Sat, 04 Apr 2015 15:58:09 GMT
------Value: Sat
------Value: 04 Apr 2015 15:58:09 GMT
--Header-----------------------------------------
----Key: Content-Type
----RawValue: text/html; charset=utf-8
------Value: text/html
------Parameter: charset  <-|->  utf-8
--Header-----------------------------------------
----Key: Transfer-Encoding
----RawValue: chunked
------Value: chunked
--Header-----------------------------------------
----Key: Connection
----RawValue: Keep-Alive
------Value: Keep-Alive
--Header-----------------------------------------
----Key: Vary
----RawValue: Accept-Encoding
------Value: Accept-Encoding
--Header-----------------------------------------
----Key: Set-Cookie
----RawValue: BAIDUID=6559C87B89AA0DF65B20DAD5A247F03F:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
------Value: BAIDUID  <-|->  6559C87B89AA0DF65B20DAD5A247F03F:FG=1
------Parameter: expires  <-|->  Thu
------Value: 31-Dec-37 23:55:55 GMT
------Parameter: max-age  <-|->  2147483647
------Parameter: path  <-|->  /
------Parameter: domain  <-|->  .baidu.com
--Header-----------------------------------------
----Key: Set-Cookie
----RawValue: BIDUPSID=6559C87B89AA0DF65B20DAD5A247F03F; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
------Value: BIDUPSID  <-|->  6559C87B89AA0DF65B20DAD5A247F03F
------Parameter: expires  <-|->  Thu
------Value: 31-Dec-37 23:55:55 GMT
------Parameter: max-age  <-|->  2147483647
------Parameter: path  <-|->  /
------Parameter: domain  <-|->  .baidu.com
--Header-----------------------------------------
----Key: Set-Cookie
----RawValue: BDSVRTM=0; path=/
------Value: BDSVRTM  <-|->  0
------Parameter: path  <-|->  /
--Header-----------------------------------------
----Key: Set-Cookie
----RawValue: BD_HOME=0; path=/
------Value: BD_HOME  <-|->  0
------Parameter: path  <-|->  /
--Header-----------------------------------------
----Key: Set-Cookie
----RawValue: H_PS_PSSID=12610_12939_1466_12658_13074_12824_12867_13321_12692_8502_12723_12737_13325_13202_13161_13256_8498_10634; path=/; domain=.baidu.com
------Value: H_PS_PSSID  <-|->  12610_12939_1466_12658_13074_12824_12867_13321_12692_8502_12723_12737_13325_13202_13161_13256_8498_10634
------Parameter: path  <-|->  /
------Parameter: domain  <-|->  .baidu.com
--Header-----------------------------------------
----Key: P3P
----RawValue: CP=" OTI DSP COR IVA OUR IND COM "
------Value: CP  <-|->   OTI DSP COR IVA OUR IND COM
--Header-----------------------------------------
----Key: Cache-Control
----RawValue: private
------Value: private
--Header-----------------------------------------
----Key: Cxy_all
----RawValue: baidu+df8e0199b72d42dc2e3ab30a7b65b584
------Value: baidu+df8e0199b72d42dc2e3ab30a7b65b584
--Header-----------------------------------------
----Key: Expires
----RawValue: Sat, 04 Apr 2015 15:58:06 GMT
------Value: Sat
------Value: 04 Apr 2015 15:58:06 GMT
--Header-----------------------------------------
----Key: X-Powered-By
----RawValue: HPHP
------Value: HPHP
--Header-----------------------------------------
----Key: Server
----RawValue: BWS/1.1
------Value: BWS/1.1
--Header-----------------------------------------
----Key: BDPAGETYPE
----RawValue: 1
------Value: 1
--Header-----------------------------------------
----Key: BDQID
----RawValue: 0xe11ae74b0002f87c
------Value: 0xe11ae74b0002f87c
--Header-----------------------------------------
----Key: BDUSERID
----RawValue: 0
------Value: 0

 

 

你可能感兴趣的:(HTTP)