Java 中的持久化 HttpURLConnection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3304006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:55:17  来源:igfitidea点击:

Persistent HttpURLConnection in Java

javahttpurlconnectionpersistent

提问by Githaron

I am trying to write a java program that will automatically download and name some of my favorite web comics. Since I will be requesting multiple objects from the same domain, I wanted to have a persistent http connection that I could keep open until all the comics have been downloaded. Below is my work-in-progress. How do I make another request from the same domain but different path without opening a new http connection?

我正在尝试编写一个 Java 程序,该程序将自动下载并命名一些我最喜欢的网络漫画。由于我将从同一个域中请求多个对象,因此我希望有一个持久的 http 连接,我可以保持打开状态,直到所有漫画都下载完毕。下面是我正在进行的工作。如何在不打开新的 http 连接的情况下从同一域但不同路径发出另一个请求?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ComicDownloader
{
    public static void main(String[] args)
    {
        URL url = null;
        HttpURLConnection httpc = null;
        BufferedReader input = null;

        try
        {
            url = new URL("http://www.cad-comic.com/cad/archive/2002");
            httpc = (HttpURLConnection) url.openConnection();
            input = new BufferedReader(new InputStreamReader(httpc.getInputStream()));
            String inputLine;

            while ((inputLine = input.readLine()) != null)
            {
                System.out.println(inputLine);
            }

            input.close();
            httpc.disconnect();
        }
        catch (IOException ex)
        {
            System.out.println(ex);
        }
    }
}

采纳答案by yclian

According to the documentation here, HTTP persistence is being handled transparently in Java, although it gives you the options to control it too via http.keepAliveand http.maxConnectionssystem properties.

根据该文档在这里,HTTP持久性被透明地在Java中处理,但它给你通过选项来控制它太http.keepAlivehttp.maxConnections系统属性。

However,

然而,

The current implementation doesn't buffer the response body. Which means that the application has to finish reading the response body or call close() to abandon the rest of the response body, in order for that connection to be reused. Furthermore, current implementation will not try block-reading when cleaning up the connection, meaning if the whole response body is not available, the connection will not be reused.

当前实现不缓冲响应正文。这意味着应用程序必须完成读取响应正文或调用 close() 以放弃响应正文的其余部分,以便重用该连接。此外,当前的实现在清理连接时不会尝试块读取,这意味着如果整个响应主体不可用,连接将不会被重用。

Take a look at the link and see if it really helps you.

看看链接,看看它是否真的对你有帮助。

回答by allen zhang

According to this link http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html, HTTP connection reuse is enabled by default, you can use Wireshark to check the interactions between your client and server. The first request contains TCP and SSL handshakes(if your request is https), the subsequent requests fired in the keep-alive time, contains no TCP and SSL handshakes, just application data transfers.

根据这个链接http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html,默认情况下启用 HTTP 连接重用,您可以使用 Wireshark 检查您之间的交互客户端和服务器。第一个请求包含 TCP 和 SSL 握手(如果您的请求是 https),后续请求在保持活动时间内触发,不包含 TCP 和 SSL 握手,仅包含应用程序数据传输。