Java Jsoup下载torrent文件



我遇到问题,我想连接到此网站(https://ww2.yggtorrent.is)下载torrent文件。我已经制定了一个由Jsoup连接到网站的方法,Jsoup运行良好,但当我尝试使用它下载torrent文件时,网站会返回"您必须连接到下载文件"。

这是我要连接的代码:

Response res = Jsoup.connect("https://ww2.yggtorrent.is/user/login")
.data("id", "<MyLogin>", "pass", "<MyPassword>")
.method(Method.POST)
.execute();

这是我下载文件的代码

Response resultImageResponse = Jsoup.connect("https://ww2.yggtorrent.is/engine/download_torrent?id=285633").cookies(cookies)
.ignoreContentType(true).execute();
FileOutputStream out = (new FileOutputStream(new java.io.File("toto.torrent")));
out.write(resultImageResponse.bodyAsBytes());
out.close();

我测试了很多东西,但现在我一无所知。

您在代码中唯一没有向我们展示的是从响应中获取cookie。我希望你这样做是正确的,因为你用它们来提出第二个请求。

这个代码看起来像你的,但有一个我如何获取cookie的例子。我还添加了referer头。它成功地为我下载了该文件,并正确识别了它:

// logging in
System.out.println("logging in...");
Response res = Jsoup.connect("https://ww2.yggtorrent.is/user/login")
.timeout(10000)
.data("id", "<MyLogin>", "pass", "<MyPassword>")
.method(Method.POST)
.execute();
// getting cookies from response
Map<String, String> cookies = res.cookies();
System.out.println("got cookies: " + cookies);
// optional verification if logged in
System.out.println(Jsoup.connect("https://ww2.yggtorrent.is").cookies(cookies).get()
.select("#panel-btn").first().text());
// connecting with cookies, it may be useful to provide referer as some servers expect it
Response resultImageResponse = Jsoup.connect("https://ww2.yggtorrent.is/engine/download_torrent?id=285633")
.referrer("https://ww2.yggtorrent.is/engine/download_torrent?id=285633")
.cookies(cookies)
.ignoreContentType(true)
.execute();
// saving file
FileOutputStream out = (new FileOutputStream(new java.io.File("C:/toto.torrent")));
out.write(resultImageResponse.bodyAsBytes());
out.close();
System.out.println("done");

最新更新