加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java中持久的HttpURLConnection

发布时间:2020-12-14 16:46:30 所属栏目:Java 来源:网络整理
导读:我正在尝试编写一个 Java程序,它将自动下载并命名一些我最喜欢的网络漫画.因为我将要从同一个域请求多个对象,所以我想要一个持久的http连接,我可以保持打开,直到所有的漫画都被下载.以下是我的工作进展.如何在不打开新的http连接的情况下从同一个域进行另一
我正在尝试编写一个 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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读