无法解析的StackExchange API响应

我编写了一个小程序来分析我的StackExchange API中的配置文件数据,但是api会向我发送unsarse- / unreadable数据。

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

\ u001f \ B \ 0 \ 0 \ 0 \ 0 \ 0 \ U0004 \ 0mRMo0 \ F /:d $ C’^ {/ \ u0006 \ u0018G> \我\ u0015 \ U0004݀d> GRL’o \ u0004G%JP \ u001c-EM> 0Xbm〜 \ u0018tk \ u0014M] rdLGv0〜FJ = 1 \ u00031I> kTRA \“(/ +; NL \ u0018 2 H \ u0014P藄XaLw#3 \ U0002 + \ u007f \ u0010 \ u000fp】v \ u007f \吨ڧ\ nf “\ u0018 \00ƺ 1x#j ^- c AX\t \u001aT @ qj \u001aU7 \ u0014 \”\ a ^ \b #\ u001eQG%Y \吨חq00K \ AV \ u0011 {ظ\ u0005 \“\ u001d + | \ u007f’\ u0016〜 8 \ u007f \ U0001-H] O \ u007fVo \ u007f \ U0001〜Y \ U0003 \ U0002 \ 0 \ 0

想要的数据:(从我的浏览器复制粘贴)

{ “物品”:[{ “badge_counts”,{ “青铜”:987, “银”:654, “金”:321}, “ACCOUNT_ID” 123456789 “is_employee”:假 “LAST_MODIFIED_DATE”:1250612752” last_access_date “:1250540770,” 年龄 “:0,” reputation_change_year “:987,” reputation_change_quarter “:654,” reputation_change_month “:321,” reputation_change_week “:98,” reputation_change_day “:76,” 信誉 “:9876,” CREATION_DATE” :1109670518,“user_type”:“registered”,“user_id”:123456789,“accept_rate”:0,“location”:“Australia”,“website_url”:“ http://example.org ”,“link”:“ http://example.org/username“,”profile_image“:”http://example.org/username/icon.png“,”display_name“:”username“}],”has_more“:false,”quota_max “ :300, “quota_remaining”:300}

我写了这个(扩展名)方法从互联网上下载一个字符串:

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获取可读数据? 有什么遗漏?

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

 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; } 

你可以看到 – 你使用的编码是错误的 –

在此处输入图像描述