c# https请求,协议不被支持, http没问题

KevinJHoo 2017-10-16 11:37:23
发现XP,win10都有这个问题,WIN7 ,WIN8未统计,哪位大神知道的?代码如下:

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;
}
...全文
652 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
KevinJHoo 2017-10-16
  • 打赏
  • 举报
回复
    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;
        }

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧