我解析了一个种子,然后这个种子的info_hash是33539c16ef7240c53ec803282de3578a13985f8e,但是如果用这个hash去请求tracker服务器的时候,一直提示服务器400,后来我抓BitComet的包的时候,发现BitComet发送的info_hash是3S%9C%16%EFr%40%C5%3E%C8%03%28-%E3W%8A%13%98_%8E.能请教下这个编码是什么吗?然后通过某个大佬的给的官网地址 找到了这么一段话
The tracker is an HTTP/HTTPS service which responds to HTTP GET requests. The requests include metrics from clients that help the tracker keep overall statistics about the torrent. The response includes a peer list that helps the client participate in the torrent. The base URL consists of the "announce URL" as defined in the metainfo (.torrent) file. The parameters are then added to this URL, using standard CGI methods (i.e. a '?' after the announce URL, followed by 'param=value' sequences separated by '&').
Note that all binary data in the URL (particularly info_hash and peer_id) must be properly escaped. This means any byte not in the set 0-9, a-z, A-Z, '.', '-', '_' and '~', must be encoded using the "%nn" format, where nn is the hexadecimal value of the byte. (See RFC1738 for details.)
For a 20-byte hash of \x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a,
The right encoded form is %124Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A
,但是对这段话有点理解不了,有大佬来指导下吗
可以这样理解,你得到的 33539c16ef7240c53ec803282de3578a13985f8e 是种子 info_hash 的十六进制字符串形式,请求 tracker 服务器的时候是需要的是 二进制的info_hash 再urlEncode一遍。
如果你用的是别人的类库解析的种子,里面一般会有个 byte[] 型的infoHash,直接在这个上面urlEncode就得到了。(如果没有,那就先把十六进制的info_hash字符串转为二进制的byte[]数组再urlEncode,ps:也或许有更好的办法)
ps: 不是这方面的大牛,只是对你这问题比较感兴趣就查了些资料,做了个验证。
info_hash转成ascii码,然后urlencode