栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从HttpURLConnection读取完整响应?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从HttpURLConnection读取完整响应?

无法直接使用来转储完整的HTTP响应

HttpURLConnection
,但是您可以使用其各种方法来重构它。例如,

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<String, List<String>> map = httpURLConnection.getHeaderFields();for (Map.Entry<String, List<String>> entry : map.entrySet()){    if (entry.getKey() == null)         continue;    builder.append( entry.getKey()).append(": ");    List<String> headerValues = entry.getValue();    Iterator<String> 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 OKX-frame-Options: SAMEORIGINTransfer-Encoding: chunkedDate: Tue, 07 Jan 2014 16:06:45 GMTP3P: 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=blockExpires: -1Alternate-Protocol: 80:quicSet-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.caContent-Type: text/html; charset=ISO-8859-1Server: gwsCache-Control: private, max-age=0

然后,您可以获取

InputStream
并打印其内容。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386838.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号