Собственно пытаюсь скачать с anidub'a торрент файл. Авторизация проходит и получаю куки, но в итоге на выходе получаю файл с кодом страницы. В чем может быть проблема?
Код:
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");
}