private HttpClientHandler getHandler() { string cacheKey = "handlerChche"; HttpClientHandler handler = null; //cache.GetCache<HttpClientHandler>(cacheKey); if (handler == null) { handler = new HttpClientHandler { ClientCertificateOptions = ClientCertificateOption.Manual, SslProtocols = SslProtocols.Tls12 }; //获取证书路径 //商户私钥证书,用于对请求报文进行签名 handler.ClientCertificates.Add(new X509Certificate2(_ecrtpath, "123456", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet)); //handler.ClientCertificates.Add(new X509Certificate2(CertHelper.GetDebugPath(), CertHelper.GetPwd(), X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet)); handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls; handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true; //cache.SetCache(cacheKey, handler); } return handler; }
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null) { postData = postData ?? ""; using (HttpClient client = new HttpClient(getHandler())) { client.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) client.DefaultRequestHeaders.Add(header.Key, header.Value); } using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) { if (contentType != null) httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); //HttpResponseMessage response = client.PostAsync(url, httpContent).Result; //return response.Content.ReadAsStringAsync().Result; return client.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync().Result; } } }
标签:SslProtocols,请求,证书,C#,client,handler,new,null,string From: https://www.cnblogs.com/gxivwshjj/p/18029049