.NET 응용 프로그램에서 Torrent 파일이 아닌 토렌트 파일을 다운로드하려고합니다.WebClient를 사용하여 토런트 파일을 다운로드하면 파일이 손상됩니다.
다음 코드를 사용하면 .torrent가 아닌 다른 파일에서도 작동합니다. 결과 파일은 브라우저를 통해 파일을 다운로드 할 때보 다 약 1-3kb 작습니다. 토런트 클라이언트에서 열면 토런트가 손상되었다고합니다.
WebClient web = new WebClient();
web.Headers.Add("Content-Type", "application/x-bittorrent");
web.DownloadFile("http://kat.ph/torrents/linux-mint-12-gnome-mate-dvd-64-bit-t6008958/", "test.torrent");
브라우저에서 URL을 열면 파일이 올바르게 다운로드됩니다.
왜 이런 일이 일어날까요? 파일을 올바르게 다운로드 할 수있는 WebClient 대신 좋은 방법이 있습니까?
편집 : 나는 웹 클라이언트뿐만 아니라이 시도했습니다, 그것은 똑같은 결과 : 서버가 내용이 GZIP에 의해 압축하고 파일이 압축 된 콘텐츠를 다운로드 반환
private void DownloadFile(string url, string file)
{
byte[] result;
byte[] buffer = new byte[4096];
WebRequest wr = WebRequest.Create(url);
wr.ContentType = "application/x-bittorrent";
using (WebResponse response = wr.GetResponse())
{
using (Stream responseStream = 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);
}
}
}
}
}
HttpWebRequest를 당신에게 다운로드를 세밀하게 제어 할 수를 ... 난 당신이 비록 토런트에 대해 구성 할 거라고 어떤 옵션을 모르겠어요. – McGarnagle
감사합니다. 결과에서 차이가없는 사실을 실제로 시도해 보았습니다. 나는 그 코드를 편집했다. 당신이 무엇인가를 보았다면 나는 그것을 시도하는데 잘못했다. – Khalos
다른 바이너리 파일을 다운로드하는 것보다 .torrent 파일을 다운로드 할 때 차이가 없어야합니다. –