【发布时间】:2014-01-07 15:51:38
【问题描述】:
我在 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 中我只收到了内容,有可能有一些带有整个 reposne 的流?
【问题讨论】:
-
不,
HttpUrlConnection无法做到这一点。但是你可以通过获取响应码、响应消息和所有的headers来模拟它。