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; } }