Java中持久的HttpURLConnection

前端之家收集整理的这篇文章主要介绍了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);
        }
    }
}

解决方法

根据 documentation here,HTTP持久性在Java中被透明化处理,尽管它还提供了通过http.keepAlive和http.maxConnections系统属性控制它的选项.

然而,

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.

看看链接,看看它是否真的有助于你.

原文链接:https://www.f2er.com/java/124326.html

猜你在找的Java相关文章