urlconnection 获取响应头_java – 如何从HttpURLConnection中读取完整的响应?

没有办法使用HttpURLConnection直接转储完整的HTTP响应,但是可以使用它的各种方法重构它.例如,

HttpURLConnection httpURLConnection;

URL url = new URL("http://www.google.com");

httpURLConnection = (HttpURLConnection) url.openConnection();

StringBuilder builder = new StringBuilder();

builder.append(httpURLConnection.getResponseCode())

.append(" ")

.append(httpURLConnection.getResponseMessage())

.append("\n");

Map> map = httpURLConnection.getHeaderFields();

for (Map.Entry> entry : map.entrySet())

{

if (entry.getKey() == null)

continue;

builder.append( entry.getKey())

.append(": ");

List headerValues = entry.getValue();

Iterator it = headerValues.iterator();

if (it.hasNext()) {

builder.append(it.next());

while (it.hasNext()) {

builder.append(", ")

.append(it.next());

}

}

builder.append("\n");

}

System.out.println(builder);

版画

200 OK

X-Frame-Options: SAMEORIGIN

Transfer-Encoding: chunked

Date: Tue, 07 Jan 2014 16:06:45 GMT

P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."

X-XSS-Protection: 1; mode=block

Expires: -1

Alternate-Protocol: 80:quic

Set-Cookie: NID=67=OIu8_xhcxE-UPCSfIoTINvRyOe4ALVhIqan2NUI6LMdRkSJHTPGvNkYeYE--WqPSEPK4c4ubvmjWGUyFgXsa453KHavX9gUeKdzfInU2Q25yWP3YtMhsIhJpUQbYL4gq; expires=Wed, 09-Jul-2014 16:06:45 GMT; path=/; domain=.google.ca; HttpOnly, PREF=ID=4496ed99b812997d:FF=0:TM=1389110805:LM=1389110805:S=jxodjb3UjGJSZGaF; expires=Thu, 07-Jan-2016 16:06:45 GMT; path=/; domain=.google.ca

Content-Type: text/html; charset=ISO-8859-1

Server: gws

Cache-Control: private, max-age=0

然后,您可以获得InputStream并打印其内容.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值