当前位置: 首页 > 面试题库 >

如何从HttpURLConnection读取完整响应?

韩博厚
2023-03-14
问题内容

我在andorid中制作了一些代理服务器来修改http标头,它可以正常工作,但是我必须将完整的响应转发到“顶层”。
如何从HttpURLConnection读取整个响应(所有标头,内容,所有内容)?

HttpURLConnection httpURLConnection;
URL url = new URL(ADDRESS);
httpURLConnection = (HttpURLConnection) url.openConnection();
// add headers, write output stream, flush
if (httpURLConnection.getResponseCode() == HttpsURLConnection.HTTP_OK)
{
    Map<String, List<String>> map = httpURLConnection.getHeaderFields();
    System.out.println("Printing Response Header...\n");

    for (Map.Entry<String, List<String>> entry : map.entrySet())
    {
        System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue());
    }

    return new DataInputStream(httpURLConnection.getInputStream());
}

在getInputStream中,我仅收到内容,是否有可能包含某些内容?


问题答案:

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



 类似资料:
  • 在过去的几天里做了一些阅读后,我已经取得了一些进展,下面是我想出的代码: 主要活动: HTTPRequest 没有错误,一切运行正常,但问题是-我已经建立了这个代码作为一个测试,如果我可以登录我试图登录的网站,但我无法从中获得任何信息。在我按下按钮后,似乎发生了什么事情,我发送到用户界面线程的输入流给了我这个:“java.io.BufferedInputStream@afe19b8”,每次按下按钮

  • 我创建了一个反向代理,如下所示: 并从main调用它: 它在客户端可以正常工作,但是我想阅读代理的响应,我怎么做?

  • 我想通过连接到另一个url再次解析从解析中获得的值。我怎么修理它? 从scrapy导入蜘蛛从scrapy.selector导入选择器 从堆栈.items导入堆栈项 类StackSpider(Spider):name=“stack”allowed_domains=[“*”]global n#n=1997 start_url=['https://www.melon.com/chart/age/list

  • 问题内容: 我有两个Python脚本。一种使用Urllib2库,另一种使用Requests库。 我发现请求更容易实现,但是找不到urlib2的等效函数。例如: 建立完发布网址后,请给我内容-我正尝试连接到vcloud Director api实例,并且响应显示了我有权访问的端点。但是,如果我按以下方式使用请求库.... .... the和不返回任何内容,即使请求后调用中的状态代码等于200。 为什

  • 问题内容: 我正在使用RestTemplate.postForObject将信息发布到Web服务。除了结果字符串,我还需要响应头中的信息。有什么办法可以做到这一点? 问题答案: 好吧,我终于明白了。交换方法正是我所需要的。它返回包含完整标头的HttpEntity。

  • Axios 0.17.1 响应的console.log为 {data:“{”error“:”name必须输入多个…null [“ispipe”:protected]=>null}}“,状态:203,statustext:”非权威信息“,标题:{…},配置:{…},…}配置:{adapter:f,转换请求:{…},转换响应:{…},超时:0,xsrfcookiename:”xsrf-token“,…