无法直接使用来转储完整的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并打印其内容。



