Java 如何从HttpURLConnection读取完整响应?

Java 如何从HttpURLConnection读取完整响应?,java,httpurlconnection,Java,Httpurlconnection,我在android中创建了一些代理服务器来修改http头,它工作正常,但我必须将完整响应转发到“顶层”。 如何从HttpURLConnection读取整个响应(所有标题、内容、所有内容) HttpURLConnection httpURLConnection; URL url = new URL(ADDRESS); httpURLConnection = (HttpURLConnection) url.openConnection(); // add headers, write output

我在android中创建了一些代理服务器来修改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());
}
HttpURLConnection-HttpURLConnection;
URL=新的URL(地址);
httpURLConnection=(httpURLConnection)url.openConnection();
//添加标题、写入输出流、刷新
if(httpURLConnection.getResponseCode()==HttpsURLConnection.HTTP\u确定)
{
Map Map=httpURLConnection.getHeaderFields();
System.out.println(“打印响应头…\n”);
对于(Map.Entry:Map.entrySet())
{
System.out.println(“Key:+entry.getKey()+”,Value:+entry.getValue());
}
返回新的DataInputStream(httpURLConnection.getInputStream());
}

在getInputStream中,我只接收内容,可能有一些流包含整个reposne?

无法直接使用
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<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);

然后,您可以获取
InputStream
并打印其内容。

当我开始寻找类似问题时,我并不清楚,所以我找到了解决方案

阅读身体反应:

readFullyAsString(connection.getInputStream(), "UTF-8");
这来自:


不,使用
HttpUrlConnection
无法做到这一点。但是您可以通过获取响应代码、响应消息和所有标题来模拟它。我正在尝试获取inputStream,但当我将流打印为文本时,它不是完整的文本。我想它只下载了一半。你的代码没有建立连接。httpURLConnection.connect();缺少。@错误如果它没有建立连接,它如何得到响应(代码、标题、正文)?不,这段代码显然确实建立了连接,它只是在
getResponseCode()
调用中隐式地建立了连接,该调用在内部调用
getInputStream()
。我几乎看不到源代码!我只是想看看doc不幸的是doc确实强调了这一点,这对未来的读者来说是正确的伟大的答案,对我帮助很大。谢谢。这并不能完全回答这个问题。问题是如何获得包括HTTP头的完整响应。这只是回答了如何读取InputStream,这是最简单的部分。请参阅此处,了解适用于Java 8的单行程序解决方案:
readFullyAsString(connection.getInputStream(), "UTF-8");
public String readFullyAsString(InputStream inputStream, String encoding) throws IOException {
        return readFully(inputStream).toString(encoding);
    }

    private ByteArrayOutputStream readFully(InputStream inputStream) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return baos;
    }