Java 中的持久 HttpURLConnection
我正在尝试编写一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据此处的文档, HTTP 持久性在 Java 中是透明处理的,尽管它也为您提供了通过 http.keepAlive 和 http.maxConnections 系统属性来控制它的选项。
然而,
查看该链接,看看它是否真的对您有帮助。
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
andhttp.maxConnections
system properties.However,
Take a look at the link and see if it really helps you.
根据此链接 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.
尽管 HttpURLConnection 默认启用 keep-alive,但不能保证 HttpURLConnection 对多个 HTTP 请求使用相同的 TCP 连接。我在编写 HTTPS 客户端应用程序时遇到了同样的问题。通过使用 SSLContext、SSLSocketFactory 和 HttpsURLConnection 的单个实例解决了此问题。
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.