在我正在研究的项目中,我需要连接到跟踪器URL并获取当前的对等方列表。但是,我认为我当前正在以错误的格式发送info_hash,导致服务器关闭了我的连接。
public void sendConnectionRequest() {
byte[] b = new byte[20];
new Random().nextBytes(b);
shaPieces = Base64.getEncoder().encodeToString(
shaPieces.getBytes(StandardCharsets.UTF_8));
try {
HttpGet request = new HttpGet(announceURL);
URI uri = new URIBuilder(request.getURI())
.setParameter("info_hash", shaPieces)
.setParameter("peer_id", b.toString())
.setParameter("port", "6910")
.setParameter("uploaded", "0")
.setParameter("downloaded", "0")
.setParameter("left", "670040064")
.setParameter("compact", "1")
.setParameter("event", "started")
.build();
request.setURI(uri);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(request);
InputStream input = response.getEntity().getContent();
Scanner inputStream = new Scanner(input);
String content = "";
while (inputStream.hasNextLine()) {
content += inputStream.nextLine();
}
System.out.println(content);
} catch (URISyntaxException | ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我收到的错误:
Dec 16, 2019 3:25:19 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://tracker.archlinux.org:6969: Connection reset by peer: socket write error
Dec 16, 2019 3:25:19 PM org.apache.http.impl.execchain.RetryExec execute
这是我尝试从.torrent文件转换信息哈希的方法:
shaPieces = DigestUtils.shaHex((String) fileInfo.get("pieces"));
最后,这是我在参数中使用的sha1哈希(转换后):
6363296ae1518d978fc30cdef14803bac6e77cc0
谢谢!