Java Jsoup downloading torrent file

♀尐吖头ヾ 提交于 2020-01-05 08:42:25

问题


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".

Here is my code to connect:

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

and here is my code to download file

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.


回答1:


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.

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


来源:https://stackoverflow.com/questions/51546943/java-jsoup-downloading-torrent-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!