通过C#读取网页中的json列表数据
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream myResponseStream = request.GetRequestStream();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string retStrings = myStreamReader.ReadToEnd();//这行代码报错
myStreamReader.Close();
myResponseStream.Close();
if (response != null)
{
response.Close();
}
if (request != null)
{
request.Abort();
}
将报错的这行代码改为 string retStrings=myStreamReader.Read();
然后将retStrings反序列化后是原本网页中的json数据吗
网页中的json数据有列表数据
这是 POST 请求,需要先写 post 数据
using(var sw = new StreamWriter(webRequest.GetRequestStream()))
{
sw.Write(postData);
}
参考 https://www.cnblogs.com/dudu/archive/2012/04/28/webrequest_post_google_oauth.html
好的,知道了,谢谢
报错又是报什么错?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.zhihu.com");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if(response.StatusCode == HttpStatusCode.OK)
{
var stream= response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
var txt=sr.ReadToEnd();
Console.WriteLine(txt);
}
以上是标准写法,另外HttpWebRequest类已经过时,推荐使用HttpClient类,更好用更强大,net framework 4.5以后都支持。
报错:
{{ "error": "在加载列表数据时发生错误!"}}
好的,知道了