我遇到一个问题,我想连接到这个网站(https://ww2.yggtorrent.is)来下载torrent文件。我已经做了一个方法来连接到网站的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();
我测试了很多东西,但现在我一无所知。
发布于 2018-07-27 06:33:53
在您的代码中唯一没有向我们展示的是从response中获取cookie。我希望您这样做是正确的,因为您使用它们来发出第二个请求。
这段代码看起来很像你的代码,但是包含了我如何获取cookie的示例。我还添加了referer标头。它成功地为我下载了该文件,并且utorrent正确地识别了它:
// 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");
https://stackoverflow.com/questions/51546943
复制相似问题