[C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书

2025-05-18 15:55:37
推荐回答(1个)
回答(1):

url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp。之后我们就可以///构建请求的HttpWebRequest对象HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create( �0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2 strValidatePageURL); /// 从本地文件中加载证书hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c:\\motor.https.pem.cer")); 这是一个较简单的办法。如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置hwrRequest.KeepAlive = true; 如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null。所以,。。。