我在Java中有一段代码创建HTTPUrlConnection,发送GET并在响应代码为200时返回true,否则返回false。问题是我试图将GET发送到特定的torcache"地址",例如torcache.net/torrent/info-hash-here.torrent(info hash是函数中的参数)。而且我不断得到IOException。任何可能出错的想法 - 我用完了。 代码在这里:
public static boolean check(String infoHash) {
final String fileURL = "http://torcache.net/torrent/" + infoHash.toUpperCase() + ".torrent";
if(connectAndGetResponseCode(fileURL) == 200) {
return true;
} else {
return false;
}
}
其中方法connectAndGetResponseCode是连接给定URL的标准实现(没有发布点)。我检查过,当我将GET发送到简单的torcache.net(或任何其他网站)时,它返回200.它也不是infoHash的问题 - 我也检查了(我使用了那些哈希&#34) ; 4FFA45CE7350E7AA19C90A432089662DCD2621D5"和#34; 6F3DE950627A0BDB2023D34357DA766A6AE4C8AB" - >这一个无效) 。谢谢你的任何建议。
编辑: 连接代码:
private static int connectAndGetResponseCode(String stringUrl) {
int code = 0;
try {
URL url = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
code = connection.getResponseCode();
connection.connect();
} catch (IOException e) {
System.out.println("Something went wrong with getting a response from torcache.net");
e.printStackTrace();
}
System.out.println(code);
return code;
}
stackTrace:
java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:778)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:775)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1325)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at WebChecker.connectAndGetResponseCode(WebChecker.java:14)
at WebChecker.check(WebChecker.java:26)
at WebChecker.main(WebChecker.java:37)
编辑#2 - wget结果
我在真实和无效的哈希上做过。
wget http://torcache.net/torrent/A633F505781CE03576E5B0F963793912D6927F12.torrent
--2015-11-15 19:50:30-- http://torcache.net/torrent/A633F505781CE03576E5B0F963793912D6927F12.torrent
解决torcache.net(torcache.net)... 94.242.255.194,109.163.226.148,95.215.61.199 连接到torcache.net(torcache.net)| 94.242.255.194 |:80 ...已连接。 发送HTTP请求,等待响应... 404 Not Found 2015-11-15 19:50:33错误404:未找到。
(假的,所以它的404是预期的)
wget http://torcache.net/torrent/4FFA45CE7350E7AA19C90A432089662DCD2621D5.torrent
- 2015-11-15 19:51:15-- http://torcache.net/torrent/4FFA45CE7350E7AA19C90A432089662DCD2621D5.torrent 解析torcache.net(torcache.net)... 109.163.226.148,95.215.61.199,94.242.255.194 连接到torcache.net(torcache.net)| 109.163.226.148 |:80 ...已连接。 发送HTTP请求,等待响应... 200 OK 长度:15466(15K)[application / x-bittorrent] 保存到:'4FFA45CE7350E7AA19C90A432089662DCD2621D5.torrent' 100%[=============================================== ================================================== =============================>] 14.466 1,22KB / s 14s 2015-11-15 19:51:29(1,08 KB / s) - '4FFA45CE7350E7AA19C90A432089662DCD2621D5.torrent'已保存[15466/15466]
(有效哈希 - 如预期的那样200)。
所以我想如果有人想下载一个torrent文件,那就完美了。但我的观点只是检查是否可以下载它,而不是执行操作;)
答案 0 :(得分:0)
我会使用Wireshark:
重复此过程,而不是wget使用您的软件。 比较http通信并查找wget和您的软件之间是否存在任何差异(例如在标题中)
答案 1 :(得分:0)
供将来参考:问题解决了。如果有人遇到类似的问题,解决方法是:
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
Torcache必须“认为”浏览器发送了get;) 有了它,它完美地运作。 我想这个问题应该标记为已关闭/已解决。