从Weblient编码byte []的类型(带帖子)

时间:2015-11-04 20:37:15

标签: c# .net post web encoding

我正在尝试以编程方式从binsearch.com下载NZB文件。我用这段代码实现了这个目标:

private static void DownloadNZB()
{
    var url = "https://www.binsearch.info/fcgi/nzb.fcgi?q=192636313";
    System.Net.ServicePointManager.Expect100Continue = false;
    using (var client = new WebClient())
    {
        var values = new NameValueCollection
            {
                { "action", "nzb" },
                { "192636313", "checked" }
            };
        byte[] result = client.UploadValues(url, values);
        byte[] converted = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), Encoding.UTF8, result);
        string resultstring = System.Text.Encoding.UTF8.GetString(converted);
    }
}

除了编码之外,它就像魅力一样。当我下载NZB时,它包含以下标题:

<?xml version="1.0" encoding="iso-8859-1" ?>

所以我猜它是Code Page 1252(ISO 8859-1),正如它在标题中所说的那样。

此值在工作代码的最后一行设置为resultstring

  

\ u001f \ u008b \ B \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \u0003å\ u009d〔O \ W \u0015Çß\u0091ø\ u000e \ u0083 \u0091PQäãuÛ7HBÕ符\ v \ b©* LT; A \ U0003   \u001eìd\ u0092 \ u001a \ u0012; I \ u008c {ûô¬=ÈÙÃY\u0095ù\ u0093 \ u0019«ECL«μÿ3sÎ^kÿöº\u009dÇ¿ùúõ«Õ\u0097ëÍöúöæÉ\u0019Ot¶Zß&LT;»}〜}óòÉÙõöö¼ÖÔÎùlõ\u009b§ ?YEA \ u009f}ò...

这是哪种类型的编码,或者我的代码有问题?

1 个答案:

答案 0 :(得分:0)

只需解压缩返回的字节......这可以正常工作

var url = "https://www.binsearch.info/fcgi/nzb.fcgi?q=192636313";
System.Net.ServicePointManager.Expect100Continue = false;
using (var client = new WebClient())
{
    var values = new NameValueCollection
    {
        { "action", "nzb" },
        { "192636313", "checked" }
    };
    byte[] result = client.UploadValues(url, values);
    var resultstring = new StreamReader(new GZipStream(new MemoryStream(result), CompressionMode.Decompress))
                       .ReadToEnd();
}