Java 中的持久 HttpURLConnection

发布于 2024-09-10 17:00:53 字数 1079 浏览 1 评论 0原文

我正在尝试编写一个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);
        }
    }
}

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?

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

篱下浅笙歌 2024-09-17 17:00:53

根据此处的文档, HTTP 持久性在 Java 中是透明处理的,尽管它也为您提供了通过 http.keepAlive 和 http.maxConnections 系统属性来控制它的选项。

然而,

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

查看该链接,看看它是否真的对您有帮助。

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.keepAlive and http.maxConnections system properties.

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.

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

糖粟与秋泊 2024-09-17 17:00:53

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

According to this link http://docs.oracle.com/javase/8/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.

烂柯人 2024-09-17 17:00:53

尽管 HttpURLConnection 默认启用 keep-alive,但不能保证 HttpURLConnection 对多个 HTTP 请求使用相同的 TCP 连接。我在编写 HTTPS 客户端应用程序时遇到了同样的问题。通过使用 SSLContext、SSLSocketFactory 和 HttpsURLConnection 的单个实例解决了此问题。

public class MyHTTPClient {
    private SSLContext mSSLContext = null;
    private SSLSocketFactory mSSLSocketFactory = null;
    private HttpsURLConnection mConnection = null;

    public void init() {
        //Setup SSL context and Socket factory here
    }

    pubblic void sendRequest() {
        URL url = new URL("https://example.com/request_receiver");
        mConnection = (HttpsURLConnection) url.openConnection();
        mConnection.setSSLSocketFactory(mSSLSocketFactory);

        // Setup request property and send request
        // Open input stream to read response
        // Close output, input streams
        
        mConnection.disconnect();
    }
}

Even though HttpURLConnection enable keep-alive by default, it is not guaranteed that HttpURLConnection uses same TCP connection for multiple HTTP requests. I faced same kind of issue when writing HTTPS client application. Solved this issue by using single instance of SSLContext, SSLSocketFactory and HttpsURLConnection.

public class MyHTTPClient {
    private SSLContext mSSLContext = null;
    private SSLSocketFactory mSSLSocketFactory = null;
    private HttpsURLConnection mConnection = null;

    public void init() {
        //Setup SSL context and Socket factory here
    }

    pubblic void sendRequest() {
        URL url = new URL("https://example.com/request_receiver");
        mConnection = (HttpsURLConnection) url.openConnection();
        mConnection.setSSLSocketFactory(mSSLSocketFactory);

        // Setup request property and send request
        // Open input stream to read response
        // Close output, input streams
        
        mConnection.disconnect();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文