使用WebClient时的System.Net.WebException:无法创建SSL/TLS安全通道

Ben*_*ual 8 c# ssl webclient

当我执行以下代码时

System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => {
    return true;
};
var webClient = new WebClient();
var s = webClient.DownloadString("https://jtlplugins.x-volution.de/api.php?apikey=yS5VS7OiG1ukiIqLzCSYuFCjeF1qSskKOQeCtVxh&do=pruefe_app&cappid=123&chardwareid=DC0D-BFEA-6F79-58DE-21E9-BA3A-B288-C46F&clizenzschluessel=123");
Run Code Online (Sandbox Code Playgroud)

我总是得到一个System.Net.WebException:无法创建SSL/TLS安全通道

当我执行这个

https://jtlplugins.x-volution.de/api.php?apikey=yS5VS7OiG1ukiIqLzCSYuFCjeF1qSskKOQeCtVxh&do=pruefe_app&cappid=123&chardwareid=DC0D-BFEA-6F79-58DE-21E9-BA3A-B288-C46F&clizenzschluessel=123
Run Code Online (Sandbox Code Playgroud)

例如,直接在Firefox或Internet Explorer中,它可以工作并返回结果.

我应该怎么做,这也是我的代码在浏览器中执行的?

我已经阅读了stackoverflow中有关此问题的其他帖子 - 但他们没有解决我的问题:-(

kmc*_*mee 18

如果你关闭fiddler(如果你打开它)并添加以下内容则异常应该消失

ServicePointManager.Expect100Continue = true;                
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

或者至少当我尝试你的代码时它对我有用

try
{
     ServicePointManager.Expect100Continue = true;                
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

     ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

     var webClient = new WebClient();

     var s = webClient.DownloadString("https://jtlplugins.x-volution.de/api.php?apikey=yS5VS7OiG1ukiIqLzCSYuFCjeF1qSskKOQeCtVxh&do=pruefe_app&cappid=123&chardwareid=DC0D-BFEA-6F79-58DE-21E9-BA3A-B288-C46F&clizenzschluessel=123");

     MessageBox.Show("Result" + s);
}
catch(Exception ex)
{ 
  MessageBox.Show(ex.Message); 
}
Run Code Online (Sandbox Code Playgroud)
  • 不安全的代码警告 - 尽管我假设您已经知道这一点并且不是您的代码获得WebException的原因,但我在此问题的原始发布后的几十年中为潜在的未来读者添加了警告.代码:

    System.Net.ServicePointManager.ServerCertificateValidationCallback =(sender,certificate,chain,errors)=> {return true; };

将忽略任何证书验证错误,因此根据定义并不完全安全.请参阅问题C#忽略证书错误?


Moh*_*MFa 6

下面是一个继承的 WebClient 类,它解决了很多像这样的常见问题......

using System;
using System.Net;
namespace YourProgram.Web
{
    public class WebClient : System.Net.WebClient
    {
        public WebClient()
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            this.container = new CookieContainer();
        }
        public WebClient(CookieContainer container)
        {
            this.container = container;
        }

        public CookieContainer CookieContainer
        {
            get { return container; }
            set { container = value; }
        }

        private CookieContainer container = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest r = base.GetWebRequest(address);
            var request = r as HttpWebRequest;
            if (request != null)
            {
                request.CookieContainer = container;
            }
            return r;
        }

        protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
        {
            WebResponse response = base.GetWebResponse(request, result);
            ReadCookies(response);
            return response;
        }

        protected override WebResponse GetWebResponse(WebRequest request)
        {
            WebResponse response = base.GetWebResponse(request);
            ReadCookies(response);
            return response;
        }

        private void ReadCookies(WebResponse r)
        {
            var response = r as HttpWebResponse;
            if (response != null)
            {
                CookieCollection cookies = response.Cookies;
                container.Add(cookies);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

享受...