C# HTTPS HttpWebRequest 需要 Framework4.5.2以上版本
public static string GetHtmlSource(string Url)
{
try
{
if (Url.StartsWith("https"))
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
}
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url.ToString());
httpWebRequest.ServicePoint.Expect100Continue = true;
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = 30000;
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)";
httpWebRequest.KeepAlive = true;
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string result = "";
using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8))
{
result = streamReader.ReadToEnd();
}
return result;
}
catch (Exception ex)
{
return ex.Message;
}
}