webclient访问HTTPS网页出现:基础连接失败,输出流出现异常


webclient在访问https的时候,可以会出现: 基础连接失败,输出流出现异常。

解决方法如下:

            //hwr = (HttpWebRequest)base.GetWebRequest(new Uri(url));
            //如果是发送HTTPS请求  
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                hwr = WebRequest.Create(url) as HttpWebRequest;
                hwr.ProtocolVersion = HttpVersion.Version10;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            }
            else
            {
                hwr = WebRequest.Create(url) as HttpWebRequest;
            }

关键的地方是这个:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

我开始搜索资料的时候,总是不行,后来偶然看到别人有说加上这一句。试了下果然成功了。(QQ:1508496092,大家碰到相关的问题可以交流一下)

你可能感兴趣的:(webclient访问HTTPS网页出现:基础连接失败,输出流出现异常)