public static string GetPage(string url, Encoding encoding)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.KeepAlive = true;
request.Timeout = 20000;
request.ContentType = "text/html; charset=UTF-8";
// request.UserAgent = "<a href=\"https://www.baidu.com/s?wd=Mozilla&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBuWnzrHN-PvDLuj0sPHmk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHnvnWczPHDzP1Dsrj0LrHTz\" target=\"_blank\" class=\"baidu-highlight\">Mozilla</a>/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2593.0 Safari/537.36";
request.Accept = "*/*";
request.KeepAlive = true;
request.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (Stream streamResponse = response.GetResponseStream())
{
StreamReader streamReader = new StreamReader(streamResponse, encoding);
return streamReader.ReadToEnd();
}
}
}
catch(Exception ex)
{
return string.Empty;
}
}
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.KeepAlive = true;
request.Timeout = 20000;
request.ContentType = "text/html; charset=UTF-8";
// request.UserAgent = "<a href=\"https://www.baidu.com/s?wd=Mozilla&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBuWnzrHN-PvDLuj0sPHmk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHnvnWczPHDzP1Dsrj0LrHTz\" target=\"_blank\" class=\"baidu-highlight\">Mozilla</a>/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2593.0 Safari/537.36";
request.Accept = "*/*";
request.KeepAlive = true;
request.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
request.AutomaticDecompression = DecompressionMethods.GZip;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (Stream streamResponse = response.GetResponseStream())
{
StreamReader streamReader = new StreamReader(streamResponse, encoding);
return streamReader.ReadToEnd();
}
}
}
catch(Exception ex)
{
return string.Empty;
}
}