java URLConnection 的几个 TCP 连接

发布于 2024-10-30 20:48:16 字数 387 浏览 4 评论 0原文

我制作了一个简单的 HTTP 客户端,它下载一组从网页解析的 URL。

我的问题是,与真正的浏览器(IE、Firefox、Chrome)相比,下载速度很慢。特别是当页面包含许多对象时。

我注意到(使用wireshark),很多时候真正的浏览器会在开始加载页面后的同一毫秒内立即建立5-10个TCP连接。这些连接将同时存在一段时间。

我的客户端还将设置并发 TCP 连接(并且它将重用 TCP 连接),但不会如此积极。我猜这是我的客户速度较慢的原因之一。

在从输入流读取之前,我尝试创建多个 URLConnections,但这对我不起作用。虽然我没有经验,所以我可能做错了。

有谁知道如何使用 URLConnection 做到这一点(模仿浏览器在 TCP 连接设置方面所做的事情)?

I have made a simple HTTP client, which downloads a set of URLs parsed from a webpage.

My problem is that the download is slow, compared to a real browser (IE, Firefox, Chrome). Especially if the page contains many objects.

I noticed (with wireshark) that many times the real browsers will setup 5-10 TCP connections within the same millisecond instantly after starting the load of a page. Those conections will then live concurrently for a period of time.

My client will also setup concurrent TCP-connections (and it will reuse TCP connections), but not at all this aggressively. I'm guessing that this is one of the reasons my client is slower.

I have tried creating several URLConnections before reading from the input stream, but this does not work for me. I am inexperienced though, so I probably do it wrong.

Does anyone know of way to do this (mimic what the browsers are doing in terms of TCP connection setup) with URLConnection?

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

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

发布评论

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

评论(2

注定孤独终老 2024-11-06 20:48:16

我建议使用 HttpClient:

http://hc.apache.org/httpcomponents-client-ga/< /a>

它支持内部连接管理、池化等。
浏览器往往有这类东西。

自从我上次使用它以来,情况可能已经发生了变化,但 UrlConnection 对于生产应用程序来说效果不佳。前任。它没有一个干净的方法来关闭它。

I recommend using HttpClient:

http://hc.apache.org/httpcomponents-client-ga/

It has support for internal connection management, pooling etc.
Browsers tend to have this sort of stuff.

Things may have changed since I last used it, but UrlConnection didn't work well for production apps. Ex. it didn't have a clean way to shut it down.

洋洋洒洒 2024-11-06 20:48:16

我还建议使用高性能网络库,例如 Apache Mina。这将自动为您创建线程池并节省您大量时间。

I would also recommend using a high performance networking library, like Apache Mina. This will automatically create thread pool for you and save you a lot of time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文