D
D
DragonSBR2016-02-26 00:33:46
C++ / C#
DragonSBR, 2016-02-26 00:33:46

How to download a .torrent file from a site?

Actually I'm trying to download a torrent file from anidub. Authorization passes and I get cookies, but in the end I get a file with the page code at the output. What could be the problem?
The code:

CookieContainer cookieContainer = new CookieContainer();

void setCookieContainer()
{
    string URI = "http://tr.anidub.com/";
    string reqString = "login=submit&login_name=sbrtorrent&login_password=123456789";

    byte[] requestData = Encoding.UTF8.GetBytes(reqString);
    CookieContainer cc = new CookieContainer();
    var request = (HttpWebRequest)WebRequest.Create(URI);
    request.Proxy = null;
    request.CookieContainer = cc;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    using (System.IO.Stream s = request.GetRequestStream())
        s.Write(requestData, 0, requestData.Length);

    using (var response = (HttpWebResponse)request.GetResponse())
    {
        StreamReader sr;
        string content;
        sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
        content = sr.ReadToEnd();
        foreach (Cookie c in response.Cookies)
            cookieContainer.Add(c);
    }
}

private void DownloadFile(string url, string file)
{
    setCookieContainer(); // авторизуемся на сайте и получаем куки
    byte[] result;
    byte[] buffer = new byte[4096];

    var wr = (HttpWebRequest)WebRequest.Create(url);
    wr.CookieContainer = cookieContainer;
    wr.ContentType = "application/x-bittorrent";
    using (WebResponse response = wr.GetResponse())
    {
        bool gzip = response.Headers["Content-Encoding"] == "gzip";
        var responseStream = gzip
                                ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)
                                : response.GetResponseStream();

        using (MemoryStream memoryStream = new MemoryStream())
        {
            int count = 0;
            do
            {
                count = responseStream.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, count);
            } while (count != 0);

            result = memoryStream.ToArray();

            using (BinaryWriter writer = new BinaryWriter(new FileStream(file, FileMode.Create)))
            {
                writer.Write(result);
            }
        }
    }
}

static void Main(string[] args)
{
    DownloadFile("http://tr.anidub.com/engine/download.php?id=13996", "1.torrent");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DragonSBR, 2016-02-26
@DragonSBR

Problem solved

void DownloadFile()
{
    setCookieContainer();
    var request = (HttpWebRequest)WebRequest.Create("http://tr.anidub.com/engine/download.php?id=14012");
    request.Proxy = null;
    request.CookieContainer = cookieContainer;
    request.Method = "POST";
    request.Referer = "http://tr.anidub.com/anime_tv/anime_ongoing/9671-polnoe-zatmenie-chernye-metki-total-eclipse-schwarzesmarken-01-iz-25.html";
    request.ContentType = "application/x-bittorrent";

    using (var response = (HttpWebResponse)request.GetResponse())
    {
        Stream str = response.GetResponseStream();

        byte[] inBuf = new byte[100000];
        int bytesReadTotal = 0;

        FileStream fstr = new FileStream("test.torrent", FileMode.Create, FileAccess.Write);

        while (true)
        {
            int n = str.Read(inBuf, 0, 100000);
            if ((n == 0) || (n == -1))
            {
                break;
            }

            fstr.Write(inBuf, 0, n);

            bytesReadTotal += n;
        }

        str.Close();
        fstr.Close();
    }
}

T
tichiy, 2016-02-26
@tichiy

of the c#

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question