Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不可解析的StackExchange API响应_C#_Webclient_Stackexchange Api_Downloadstring - Fatal编程技术网

C# 不可解析的StackExchange API响应

C# 不可解析的StackExchange API响应,c#,webclient,stackexchange-api,downloadstring,C#,Webclient,Stackexchange Api,Downloadstring,我已经编写了一个小程序来分析StackExchange API中的配置文件数据,但该API会向我返回不可解析/不可读的数据 收到的数据:(使用c#自行下载) \u001f�\b\0\0\0\0\0\0\u0004\0mRMo�0\f�/:�d$�c˱�'�^{/\u0006��\u0018G�>\我��\u0015���\u0004݀�D>�GR�我是���o��\u0004�G���%JP\u001c����-��Em>0���X�bm~�\u0018tk��\u0014�M] r�dLG�v

我已经编写了一个小程序来分析StackExchange API中的配置文件数据,但该API会向我返回不可解析/不可读的数据

收到的数据:(使用c#自行下载)

\u001f�\b\0\0\0\0\0\0\u0004\0mRMo�0\f�/:�d$�c˱�'�^{/\u0006��\u0018G�>\我��\u0015���\u0004݀�D>�GR�我是���o��\u0004�G���%JP\u001c����-��Em>0���X�bm~�\u0018tk��\u0014�M] r�dLG�v0~Fj=���1\u00031I�>kTRA\“(/+)。����;Nl\u0018�?H�\u0014��P藄�X�艾尔��W���#�3\u0002��+�\u007f���\u0010���\u000f�P�]��v\u007f���\T��ڧ�\核因子��״\u0018\u0014eƺ�_��1x#j^-�C� � AX\t���\u001aT��@qj\u001aU7�����\u0014\“\a^\b�#\u001e��QG��%�Y�\T�ח������q00K\av\u0011{ظ���\u0005\“\u001d+|\u007f���'�\u0016~��8\u007f�\u0001-h�]O\u007fV�o\u007f\u0001~Y\u0003��\u0002\0\0

需要数据:(从我的浏览器粘贴副本)

{“项目”:[{“徽章计数”,“铜”:987,“银”:654,“金”:321},“账户id”:123456789,“是员工”:假,“最后修改日期”:1250612752,“最后访问日期”:1250540770,“年龄”:0,“声誉变化年”:987,“声誉变化季度”:654,“声誉变化月”:321,“声誉变化周”:98,“声誉变化日”:76,“声誉”:9876,”创建日期:1109670518,“用户类型”:“已注册”,“用户id”:123456789,“接受率”:0,“位置”:“澳大利亚”,“网站url”:“链接”:“个人资料图像”:“显示名称”:“用户名”}],“有更多”:false,“配额最多”:300,“配额剩余”:300}

我编写了这个(扩展)方法来从internet下载字符串:

public static string DownloadString(this string link)
{
    WebClient wc = null;
    string s = null;
    try
    {
        wc = new WebClient();
        wc.Encoding = Encoding.UTF8;
        s = wc.DownloadString(link);
        return s;
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        if (wc != null)
        {
            wc.Dispose();
        }
    }
    return null;
}
然后我搜索了互联网,找到了一种下载字符串的方法,使用了其他一些策略:

public string DownloadString2(string link)
{
    WebClient client = new WebClient();
    client.Encoding = Encoding.UTF8;
    Stream data = client.OpenRead(link);
    StreamReader reader = new StreamReader(data);
    string s = reader.ReadToEnd();
    data.Close();
    reader.Close();
    return s;
}
但这两种方法都返回相同的(未读/不可解析)数据


如何从API中获取可读数据?是否缺少任何内容?

在我看来,输出是压缩的。您可以使用
GZipStream
,可以在
System.IO.Compression
中找到,以便解压缩字节

public static string DownloadString(this string link)
{
    WebClient wc = null;
    try
    {
        wc = new WebClient();
        wc.Encoding = Encoding.UTF8;
        byte[] b = wc.DownloadData(link);

        MemoryStream output = new MemoryStream();
        using (GZipStream g = new GZipStream(new MemoryStream(b), CompressionMode.Decompress))
        {
            g.CopyTo(output);
        }

        return Encoding.UTF8.GetString(output.ToArray());
    }
    catch
    {

    }
    finally
    {
        if (wc != null)
        {
            wc.Dispose();
        }
    }
    return null;
}

正如您所看到的,您使用的编码是错误的-


FWIW,
try{}
最后,如果您所做的只是重新引用异常,那么没有
catch{}
块的{}块是完全有效的。您访问的URL是什么?@PatrickHofman@JamesThorpe刚刚为调试/错误搜索添加了
throw
部分,通常为空,但谢谢。