Java Jsoup下载torrent文件 [英] Java Jsoup downloading torrent file

查看:371
本文介绍了Java Jsoup下载torrent文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,我想连接到此网站( https://ww2.yggtorrent.is )下载torrent文件.我已经通过Jsoup开发了一种方法来连接到网站,该方法可以正常工作,但是当我尝试使用它来下载torrent文件时,网站返回您必须连接才能下载文件".

I got a problem, I want to connect to this website (https://ww2.yggtorrent.is) to download torrent file. I've made a method to connect to the website by Jsoup who work well but when I try to use it to Download the torrent file, the website return "You must be connected to download file".

这是我要连接的代码:

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();

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

I've tested a lot of thing but now I have no clue.

推荐答案

您未在代码中向我们展示的唯一一件事就是从响应中获取cookie.我希望您正确执行此操作,因为您使用它们进行了第二次请求.

The only thing you didn't show us in your code is getting cookies from response. I hope you do this correctly because you use them to make second request.

此代码看起来像您的代码,但带有有关如何获取Cookie的示例.我还添加了引荐标头.它成功为我下载了该文件,并且utorrent正确识别了该文件:

This code looks like yours but with example of how I get the cookies. I also add referer header. It successfully downloads that file for me and utorrent recognizes it correctly:

    // 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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆