【发布时间】:2015-02-16 17:11:33
【问题描述】:
我正在尝试创建独立的网络服务器以编程方式搜索 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 个字节!为什么??
【问题讨论】:
-
你是如何写入文件的?
标签: java http url download httpurlconnection