使用 HttpURLConnection 的 HTTP 请求不重用 TCP 连接 [英] TCP connection is not reused for HTTP requests with HttpURLConnection

查看:36
本文介绍了使用 HttpURLConnection 的 HTTP 请求不重用 TCP 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序,它将 GET 请求发送到 URL,然后下载该页面的全部内容.

I'm have created an application which sends GET requests to a URL, and then downloads the full content of that page.

客户端发送一个 GET 到例如stackoverflow.com,并将响应转发给解析器,解析器有责任从页面中查找所有需要下载的源以及后续的 GET 请求.

The client sends a GET to e.g. stackoverflow.com, and forwards the response to a parser, which has the resposibility to find all the sources from the page that needs to be downloaded with subsequent GET requests.

下面的方法用于发送那些 GET 请求.它被连续多次调用,解析器返回 URL.大多数这些 URL 都位于同一主机上,并且应该能够共享 TCP 连接.

The method below is used to send those GET requests. It is called many times consecutively, with the URLs returned by the parser. Most of those URLs are located on the same host, and should be able to share the TCP connection.

public static void sendGetRequestToSubObject(String RecUrl)
    {
        URL url = new URL(recUrl.toString());
        URLConnection connection = url.openConnection ();
        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
    }

每次调用此方法时,都会创建一个新的 TCP 连接(使用 TCP 3 次握手),然后在该连接上发送 GET.但我想重用 TCP 连接,以提高性能.

Each time this method is called, a new TCP connection is created (with a TCP 3-way handshake) and the GET is then sent on that connection. But I want to reuse the TCP connections, to improve performance.

我猜是因为每次调用该方法时我都会创建一个新的 URL 对象,所以这就是它的工作方式......

I guess that since I create a new URL object each time the method is called, this is the way it going to work...

也许有人可以帮助我以更好的方式做到这一点?

Maybe someone can help me do this in a better way?

谢谢!

推荐答案

HttpURLConnection 重用连接 如果可以的话

为此,需要满足几个先决条件,主要是在服务器端.这些先决条件在上面链接的文章中进行了描述.

For this to work, several preconditions need to be fulfilled, mostly on the server side. Those preconditions are described in the article linked to above.

这篇关于使用 HttpURLConnection 的 HTTP 请求不重用 TCP 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆