当我调用此API时: https://api.stackexchange.com/2.2/questions?order=desc&sort=creation&site=stackoverflow
HttpClient client = new HttpClient();
ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
HttpWebRequest request = this.GetRequest("https://api.stackexchange.com/2.2/questions?order=desc&sort=creation&site=stackoverflow");
WebResponse webResponse = request.GetResponse();
var responseText = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
任何人都可以提供帮助吗?
答案 0 :(得分:0)
尝试以下代码行
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("https://api.stackexchange.com/2.2/questions?order=desc&sort=creation&site=stackoverflow");
}
答案 1 :(得分:0)
阅读内容如下。您应该在流周围创建一个StreamReader
,然后调用ReadToEnd。
using (Stream stream = webResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
var responseText = reader.ReadToEnd();
}