java - Bittorrent 跟踪器获取请求 urlencode

标签 java http bittorrent

我正在使用来自

的 torrent 文件 (ubuntu-14.04.1-desktop-amd64.iso)

http://torrent.ubuntu.com:6969/

我计算了它的信息哈希值,它与网站上的哈希值相匹配。但是当我尝试发送 GET 请求时:

http://torrent.ubuntu.com:6969/announce?info_hash=%CB%84%EF%BF%BD%EF%BF%BD%0F%29m%EF%BF%BD-l%40%EF%BF%BDz%07%EF%BF%BDx%EF%BF%BD2%3A%14&peer_id=%EF%BF%BD%07d%EF%BF%BD%EF%BF%BD%EF%BF%BDI%EF%BF%BD%5E%EF%BF%BDCo%D8%97d%7D%EF%BF%BDep%EF%BF%BD&port=6881&event=started

我得到:

d14:失败原因63:请求的下载未获授权与此跟踪器一起使用。e

我不明白我做错了什么

我以这种方式生成请求:

HttpURLConnection connection;
final String INFO_HASH = "info_hash";
final String PEER_ID = "peer_id";
final String PORT = "port";
final String EVENT = "event";

try {
    byte[] peerId = new byte[20];
    Random rnd = new Random();
    rnd.nextBytes(peerId);
    URI uri = new URIBuilder(metaInfo.getAnnounce())
        .addParameter(INFO_HASH, new String(metaInfo.getInfoHash(), "UTF-8"))
        .addParameter(PEER_ID, new String(peerId, "UTF-8"))
        .addParameter(PORT, "6881")
        .addParameter(EVENT, "started").build();
    log.info("Sending request to: " + uri.toURL().toString());
    connection = (HttpURLConnection) uri.toURL().openConnection();
} catch (MalformedURLException e) {
    throw new RuntimeException("Invalid torrent file: illegal announce in torrent file");
} catch (URISyntaxException e) {
    throw new RuntimeException("Couldn't build URI: illegal announce in torrent file");
}

最佳答案

我明白了。您需要在 ISO-8859-1 中编写 info_hash 并始终指定其他字段(上传、下载、离开)。此外,torrent.ubuntu.com还希望我指定compact和no_peer_id

代码的工作版本:

HttpURLConnection connection;
final String INFO_HASH = "info_hash";
final String PEER_ID = "peer_id";
final String PORT = "port";
final String EVENT = "event";
final String UPLOADED = "uploaded";
final String DOWNLOADED = "downloaded";
final String LEFT = "left";
final String NO_PEER_ID = "no_peer_id";
final String COMPACT = "compact";

try {
    byte[] peerId = new byte[20];
    Random rnd = new Random();
    rnd.nextBytes(peerId);

    URI uri = new URIEncodeBuilder(metaInfo.getAnnounce())
            .addParameter(INFO_HASH, new String(metaInfo.getInfoHash(), "ISO-8859-1"), "ISO-8859-1")
            .addParameter(PEER_ID, "-TO0042-0ab8e8a31019")
            .addParameter(PORT, "6881")
            .addParameter(EVENT, "started")
            .addParameter(UPLOADED, "0")
            .addParameter(DOWNLOADED, "0")
            .addParameter(LEFT, "1028128768")
            .addParameter(COMPACT, "1")
            .addParameter(NO_PEER_ID, "0").build();
    log.info("Maybe: " + uri.toURL().toString());
    connection = (HttpURLConnection) uri.toURL().openConnection();
} catch (MalformedURLException e) {
    throw new RuntimeException("Invalid torrent file: illegal announce in torrent file");
} catch (URISyntaxException e) {
    throw new RuntimeException("Couldn't build URI: illegal announce in torrent file");
}

关于java - Bittorrent 跟踪器获取请求 urlencode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28398976/

相关文章:

java - 了解执行器服务何时完成所有提交的任务的最佳方法是什么

java - 解释期间 Antlr MismatchedSetException

java - 在 for 循环中使用数组长度不安全

c# - HttpClient 错误太多请求速率限制

python - 我如何发送带有 python 和动态值的发布请求?

http - Web API GET 请求中的电子邮件地址

可以通过 bash 添加磁力链接的 Linux torrent 客户端

bittorrent - 使用 netcat ping router.utorrent.com DHT 节点

objective-c - 将 NSData 字节转换为 NSString?

java - SpringBeanAutowiringInterceptor - 配置为按名称而不是按类型 Autowiring ?