HTTPUrlConnection和.torrent文件

时间:2014-12-18 01:17:20

标签: java http url download httpurlconnection

我试图创建独立的网络服务器,以编程方式搜索torrent文件(例如来自torrentz.eu)并下载。

我通过下载单个torrent文件完全生气,似乎服务器响应使用浏览器或java不同。

这是剧本:

      connection = (HttpURLConnection)url.openConnection();

            connection.setRequestProperty("Cookie", cookies);
                    System.setProperty("http.agent", "");
                    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

                    connection.setRequestMethod("GET");
                    connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
                    connection.setRequestProperty("Connection", "keep-alive");
                    connection.setRequestProperty("Content-Language", "en-US");
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    connection.setRequestProperty("Connection", "Keep-Alive");            
                    connection.setConnectTimeout(22000);
                    connection.setReadTimeout(12000);
                    connection.setUseCaches(false);
                    connection.setDoInput(true);
                    connection.setDoOutput(true);

    connection.connect();
    respCode = connection.getResponseCode();

    if(respCode != 200){
    // do something..
    return false;
    }

    ByteArrayOutputStream list = new ByteArrayOutputStream();
stream = connection.getInputStream();

            byte[] buffer = new byte[512];
            int c;
            while ((c = stream.read(buffer)) != -1) {
                if(c > 0){
                    list.write(buffer, 0, c);
                }
            }
            list.flush();
            stream.close();

此代码适用于html,图像文件,ecc ..但是无法获取.torrent文件,它们已损坏:

示例:UBUNTU torrent, https://torcache.net/torrent/B415C913643E5FF49FE37D304BBB5E6E11AD5101/[katproxy.com]ubuntu.14.10.desktop.64bit.iso.torrent

  • 浏览器下载的.torrent文件大小:44920字节
  • java下载的.torrent文件的大小:44795字节 缺少135个字节!为什么??

1 个答案:

答案 0 :(得分:1)

发现了问题!

该文件是GZIP压缩的!可能,浏览器默认会自动解压缩...非常感谢你!