gpt4 book ai didi

Java Jsoup 下载 torrent 文件

转载 作者:行者123 更新时间:2023-11-30 06:04:51 29 4
gpt4 key购买 nike

我遇到问题,我想连接到此网站 ( 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();

我已经测试了很多东西,但现在我不知道。

最佳答案

您在代码中唯一没有向我们展示的是从响应中获取 cookie。我希望您正确执行此操作,因为您使用它们来发出第二个请求。

这段代码看起来像你的,但有我如何获取cookie的示例。我还添加了引用 header 。它成功地为我下载了该文件,并且 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");

关于Java Jsoup 下载 torrent 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51546943/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com