1.安装证书:
手动双击证书安装,过程略
2.分配权限:
在控制台中找到安装的证书,右键选择“管理私钥”,
添加自己需要的权限,如果在测试可以直接添加Everyone
3.修改代码:public static string Refund(string urlWithParams)
{ ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; //协议按需选择,不行就都试一遍 ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return true; }; HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(urlWithParams); webRequest.Method = "GET"; //查找我们导入的证书 X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); certStore.Open(OpenFlags.ReadOnly); X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindBySubjectName, "安装的证书名字", false); webRequest.ClientCertificates.Add(certCollection[0]); using (WebResponse webResponse = webRequest.GetResponse()) { var sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8); var responseData = sr.ReadToEnd();
return responseData; } }