java URLConnection 的几个 TCP 连接
我制作了一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 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.
我还建议使用高性能网络库,例如 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.