h3n*_*h3n 77 .net c# json webrequest
我需要从外部域获取json数据.我使用webrequest来获取网站的回复.这是代码:
var request = WebRequest.Create(url);
string text;
var response = (HttpWebResponse) request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么我不能得到json数据?
Mar*_*erl 71
某些API希望您在请求中提供适当的"Accept"标头以获取所需的响应类型.
例如,如果API可以返回XML和JSON中的数据,并且您需要JSON结果,则需要将该HttpWebRequest.Accept属性设置为"application/json".
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = "application/json";
Run Code Online (Sandbox Code Playgroud)
Ore*_*n A 64
您需要明确询问内容类型.
添加此行:
request.ContentType = "application/json; charset=utf-8";在适当的地方
| 归档时间: |
|
| 查看次数: |
142943 次 |
| 最近记录: |