如何在C#中使用system.net.webrequest得到JSON响应? [英] How to get json response using system.net.webrequest in c#?
本文介绍了如何在C#中使用system.net.webrequest得到JSON响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从外部域获取JSON数据。 我用的WebRequest获取从网站的响应。 这里的code:
I need to get json data from an external domain. I used webrequest to get the response from a website. Here's the code:
var request = WebRequest.Create(url);
string text;
var response = (HttpWebResponse) request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
任何人都知道,为什么我不能得到的JSON数据?
Anyone know why I can't get the json data?
推荐答案
您需要明确要求的内容类型。
You need to explicitly ask for the content type.
添加此行:
request.ContentType = "application/json; charset=utf-8";
在适当的地方
At the appropriate place
这篇关于如何在C#中使用system.net.webrequest得到JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文