本文介绍了使用System.Net.WebClient与HTTPS证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的C#Windows客户端,我有一个POST提交母舰。我想固定当然,在提交的数据,所以我支付HostGator的发给我一个SSL证书。In my C# Windows client, I have a POST submission to "the mothership". I want the data in the submits to be secured, of course, so I paid for HostGator to issue me an SSL certificate.我救离.CER文件,我建设的要求这样:I saved off the .CER file, and I'm constructing the request as such://wrapper for WebClient object to use certificate fileclass SecureWebClient : WebClient{ protected override WebRequest GetWebRequest(Uri address) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address); string certPath = @"e:\mycertificate.cer"; X509Certificate myCert = X509Certificate.CreateFromCertFile(certPath); request.ClientCertificates.Add(myCert); return request; }}//requestprivate static SecureWebClient client = new SecureWebClient();private static NameValueCollection = new NameValueCollection();nvc.Add(POST_ACTION, ACTION_CODE_LOGIN);nvc.Add(POST_EMAIL, email);nvc.Add(POST_PASSWORD, password);sResponse = System.Text.Encoding.ASCII.GetString(client.UploadValues(BASE_URL + ACTION_PAGE, nvc)); 它抛出一个System.Net.WebException:Its throwing a System.Net.WebException:基础连接已关闭:上一个发送发生意外的错误的InnerException是一个System.IO.IOException:The InnerException is a System.IO.IOException:由于没有意外的数据包格式,握手这是什么我做错了任何见解?Any insight on what I am doing wrong?推荐答案如果你不可以使用的客户的证书和您可以访问使用你的服务器的 https://开头,然后你的代码应该是这样的:If you're not using client certificates and you can access your server using https:// then your code should look like:private static WebClient client = new WebClient();private static NameValueCollection nvc= new NameValueCollection();nvc.Add(POST_ACTION, ACTION_CODE_LOGIN);nvc.Add(POST_EMAIL, email);nvc.Add(POST_PASSWORD, password);sResponse = System.Text.Encoding.ASCII.GetString(client.UploadValues(BASE_URL + ACTION_PAGE, nvc)); 只要您的 BASE_URL 使用的https:// ,那么所有的数据(向和从服务器)将被加密。As long as your BASE_URL uses https:// then all the data (to and from the server) will be encrypted. IOW使用SSL / TLS从客户机到服务器,不需要在客户端做任何特殊(含证书),除了使用的 https://开头作为方案,因为操作系统提供的一切(如可信任的根),你需要确保数据传输。IOW using SSL/TLS from a client to a server does not require the client to do anything special (with the certificate), besides using https:// as the scheme, since the operating system provides everything (e.g. trusted roots) you need to secure the data transmission. 这篇关于使用System.Net.WebClient与HTTPS证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-24 11:35