Java中的持久HttpURLConnection [英] Persistent HttpURLConnection in Java
问题描述
我正在尝试编写一个java程序,它将自动下载并命名一些我最喜欢的网络漫画。因为我将从同一个域请求多个对象,所以我希望有一个持久的http连接,我可以保持打开直到所有漫画都已下载。以下是我正在进行的工作。如何在不打开新的http连接的情况下从同一域但不同的路径发出另一个请求?
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);
}
}
}
推荐答案
根据此处的文档
,HTTP持久性正在Java中透明地处理,尽管它也为您提供了通过 http.keepAlive
和 http.maxConnections <来控制它的选项。 / code>系统属性。
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
and http.maxConnections
system properties.
然而,
当前实现没有
缓冲响应体。这意味着
应用程序必须完成
读取响应正文或调用
close()放弃
响应正文的其余部分,以便获得
要重用的连接。此外,
当前实现在清理
连接时不会尝试
块读取,这意味着如果整个
响应主体不可用,
连接将不会可以重复使用。
The current implementation doesn't
buffer the response body. Which means
that the application has to finish
reading the response body or call
close() to abandon the rest of the
response body, in order for that
connection to be reused. Furthermore,
current implementation will not try
block-reading when cleaning up the
connection, meaning if the whole
response body is not available, the
connection will not be reused.
看一下这个链接,看看它是否对你有帮助。
Take a look at the link and see if it really helps you.
这篇关于Java中的持久HttpURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!