JavaJSOUP下载torrent文件

JavaJSOUP下载torrent文件,java,download,jsoup,torrent,Java,Download,Jsoup,Torrent,我遇到一个问题,我想连接到这个网站()下载torrent文件。我已经用Jsoup的方法连接到网站,Jsoup工作得很好,但是当我尝试使用它下载torrent文件时,网站返回“你必须连接到下载文件” 以下是我要连接的代码: Response res = Jsoup.connect("https://ww2.yggtorrent.is/user/login") .data("id", "<MyLogin>", "pass", "<MyPassword>

我遇到一个问题,我想连接到这个网站()下载torrent文件。我已经用Jsoup的方法连接到网站,Jsoup工作得很好,但是当我尝试使用它下载torrent文件时,网站返回“你必须连接到下载文件”

以下是我要连接的代码:

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

我已经测试了很多东西,但现在我没有任何线索。

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

这段代码看起来像你的,但有我如何得到cookies的例子。我还添加了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");
//登录
System.out.println(“登录…”);
Response res=Jsoup.connect(“https://ww2.yggtorrent.is/user/login")
.超时(10000)
.数据(“id”、“通过”、“通过”)
.method(method.POST)
.execute();
//从响应获取cookie
映射cookies=res.cookies();
System.out.println(“获取cookies:+cookies”);
//可选验证(如果已登录)
System.out.println(Jsoup.connect(“https://ww2.yggtorrent.is“”.cookies(cookies).get()
.选择(“#面板btn”).first().text());
//与Cookie连接时,按照某些服务器的期望提供referer可能会很有用
Response resultImageResponse=Jsoup.connect(“https://ww2.yggtorrent.is/engine/download_torrent?id=285633")
.推荐人(”https://ww2.yggtorrent.is/engine/download_torrent?id=285633")
.饼干(饼干)
.ignoreContentType(true)
.execute();
//保存文件
FileOutputStream out=(新的FileOutputStream(新的java.io.File(“C:/toto.torrent”));
write(resultImageResponse.bodyAsBytes());
out.close();
系统输出打印项次(“完成”);

可能存在身份验证问题?否,因为我使用错误的密码进行了测试,它返回了一个错误,但您确定您的正确身份验证已通过?是,因为它返回了HTML代码,上面写着“欢迎回来”,非常感谢!它现在工作得很好!再次感谢!
    // 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");