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

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

问题描述

我创建了一个应用程序,向网址发送GET请求,然后下载该网页的完整内容。

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。大多数网址位于同一台主机上,应该能够共享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.

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

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