110,549
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// Web的Get方式
/// </summary>
/// <param name="Url">网址URL</param>
/// <param name="cc">CookieContainer</param>
/// <returns></returns>
public static string GetHtmlMethodForCommon(string Url, ref CookieContainer cc)
{
string strHtmlReturn = "";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
//属性配置
webRequest.AllowWriteStreamBuffering = true;
webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.MaximumResponseHeadersLength = -1;
webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, */*";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; (R1 1.5); .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2; .NET4.0C; .NET4.0E; Alexa Toolbar; u9dnfsh)";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "GET";
//如果服务器返回错误,他还会继续再去请求,不会使用之前的错误数据,做返回数据
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 200000;
ServicePointManager.DefaultConnectionLimit = 50;
webRequest.Headers.Add("Accept-Language", "zh-cn");
webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
webRequest.KeepAlive = true;
webRequest.CookieContainer = cc;
try
{
//获取服务器返回的资源
//运行三四个小时后,此处报错
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
if (webResponse.StatusCode == HttpStatusCode.RequestTimeout)//此处作超时响应判断
{
System.Threading.Thread.Sleep(1000);
return null;
}
else
{
Stream responseStream = webResponse.GetResponseStream();
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
cc.Add(webResponse.Cookies);
strHtmlReturn = reader.ReadToEnd();
}
}
}
}
catch (WebException ex)
{
strHtmlReturn = "";
throw ex;
}
catch (Exception ex)
{
strHtmlReturn = "";
throw ex;
}
finally
{
webRequest.Abort();
}
return strHtmlReturn;
}