java - HTTPUrlConnection 和 .torrent 文件

标签 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 种子, https://torcache.net/torrent/B415C913643E5FF49FE37D304BBB5E6E11AD5101/[katproxy.com]ubuntu.14.10.desktop.64bit.iso.torrent

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

最佳答案

找到问题了!

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

关于java - HTTPUrlConnection 和 .torrent 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537703/

相关文章:

java - 响应用户输入的 Applet 绘图

javac "cannot find symbol"命令行错误

java - 我的 java 代码中使用 httpconnection 出现奇怪的错误

android - 如何使用 android HttpRequest 在 Android onPause 中保存状态

获取当前 URL 的 Javascript Bookmarklet

java - 这真的是从 Java 将 void 函数传递给 Scala 方法的方法吗?

http - 域重定向到同一页面和谷歌

PHP 用 URL 替换字符串中的模式

Javascript - 从当前网址中删除查询字符串

java - Mockito When...Then Return - 返回被忽略?