110,499
社区成员
发帖
与我相关
我的任务
分享
public static string GetPage(String strURL)
{
string strResult = "";
HttpWebResponse response = null;
System.IO.StreamReader sr = null;
HttpWebRequest myHttpWebRequest = null;
try
{
if (strURL.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
myHttpWebRequest = WebRequest.Create(strURL) as HttpWebRequest;
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
myHttpWebRequest.ProtocolVersion = HttpVersion.Version11;
// 这里设置了协议类型。
//ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
myHttpWebRequest.KeepAlive = false;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.Expect100Continue = false;
}
else
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.Proxy = null;
myHttpWebRequest.ServicePoint.UseNagleAlgorithm = false;
myHttpWebRequest.ContentType = "text/html";
myHttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0;kdBrowser) Gecko/20100101 Firefox/29.0";
myHttpWebRequest.Method = "GET";
response = (HttpWebResponse)myHttpWebRequest.GetResponse();
sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")); // //utf-8
strResult = sr.ReadToEnd();
}
catch (Exception er)
{
Class.winClass.CLogFile.WriteErorrLog(er);
//MessageBox.Show(er.Message);
strResult = "";
}
finally
{
if (sr != null)
{
sr.Close();
sr = null;
}
if (response != null)
{
response.Close();
response = null;
}
}
return strResult;
}
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
public TrustAllCertificatePolicy()
{ }
public bool CheckValidationResult(ServicePoint sp,
System.Security.Cryptography.X509Certificates.X509Certificate cert,
WebRequest req, int problem)
{
return true;
}
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}