共计 583 个字符,预计需要花费 2 分钟才能阅读完成。
这里留神超时值不要设置得太小,不然会引发 System.Net.WebException 异样,值以毫秒为单位,个别设置为 5000 以上:
using System;
using System.Net;
using System.IO;
using System.Text;
string GetHttpResponse(string url, int timeout)
{HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Timeout = timeout;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string resString = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return resString;
}
相干环境:
.NET Framework 4.0
正文完