62,046
社区成员
发帖
与我相关
我的任务
分享
public static string HttpReader(string url)
{
if (url == String.Empty || !UtilsRequest.IsHttp(url))
return String.Empty;
ServicePointManager.DefaultConnectionLimit = 1000;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
string htmlCode = String.Empty;
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
try
{
webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Referer = url;
webRequest.Timeout = 30000;
webRequest.Method = "GET";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
webRequest.AllowAutoRedirect = false;
webRequest.KeepAlive = false;
//webRequest.Proxy = null;
webRequest.Accept = "*/*"; // 不加503错误
//webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
//webRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN");
webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
string contenttype = webResponse.Headers["Content-Type"];
string encoding = String.Empty;
if (contenttype.Contains("gb2312"))
encoding = "GB2312";
else if (contenttype.Contains("gbk"))
encoding = "GBK";
else
encoding = "UTF-8";
using (Stream streamReceive = webResponse.GetResponseStream())
{
using (StreamReader sr = new StreamReader(streamReceive, Encoding.GetEncoding(encoding)))
{
htmlCode = sr.ReadToEnd();
sr.Dispose();
streamReceive.Dispose();
sr.Close();
streamReceive.Close();
}
}
}
catch { }
finally
{
if (webRequest != null) webRequest.Abort();
if (webResponse != null) webResponse.Close();
}
return htmlCode;
}