Bittorrent跟踪器获取请求urlencode

时间:2015-02-08 20:27:31

标签: 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:failure reason63:Requested download is not authorized for use with this tracker.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");
}

1 个答案:

答案 0 :(得分:2)

我明白了。您需要在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");
}
相关问题