最近在写一个跟第三方对接的数据同步服务,在本地都没有问题,今天放到生产环境测试报错:

System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

对方用的是https,看错误是证书问题,查了一些资料,貌似说这个问题的比较少,所以在此总结一下,以防以后用到。

public string GetResponseData(string jsonData, string url)
{
byte[] bytes = Encoding.UTF8.GetBytes(jsonData);

//1.1 在2.0下ServicePointManager.CertificatePolicy已经过时 
//ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();

//2.0 https
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = bytes.Length;

request.ContentType = "application/json";
Stream reqstream = request.GetRequestStream();
reqstream.Write(bytes, 0, bytes.Length);
//设置连接超时时间
request.Timeout = 60000;

request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream streamReceive = response.GetResponseStream();
Encoding encoding = Encoding.UTF8;

StreamReader streamReader = new StreamReader(streamReceive, encoding);
string strResult = streamReader.ReadToEnd();
streamReceive.Dispose();
streamReader.Dispose();

return strResult;
}
return "";
}

//2.0 https

public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}

// 1.1
//internal class AcceptAllCertificatePolicy : ICertificatePolicy
//{
//public AcceptAllCertificatePolicy()
//{
//}

//public bool CheckValidationResult(ServicePoint sPoint, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest wRequest, int certProb)
//{
//return true;
//}
//}

Could not establish trust relationship for the SSL/TLS secure channel 问题解决方法的更多相关文章

  1. EX:The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

    EX:The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secu ...

  2. Web Server 使用WebClient 发送https请求 The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

    使用WebClient 发送https请求 使用WebClient发送请求时,并且是以https协议: WebClient webClient = new WebClient(); string re ...

  3. https请求时出错:Could not establish trust relationship for the SSL/TLS secure channel

    当我在用NET命名空间下获取URL的时候,提示如下错误: The underlying connection was closed: Could not establish trust relatio ...

  4. [bug]The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

    写在前面 在模拟请求的时候,如果url为https的,会报这个错误.大概错误就是:基础连接已关闭:无法建立信任关系的SSL / TLS的安全通道. The underlying connection ...

  5. WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

    关于这个异常的问题网上有很多的解决方案. 最为靠谱的有: http://www.cnblogs.com/hjf1223/archive/2007/03/14/674502.html(若因为链接而导致不 ...

  6. 请求被中止: 未能创建 SSL/TLS 安全通道,以及解决方法,即:Could not create SSL/TLS secure channel

    C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel) 以及 X509Certificate2 temp ...

  7. 偶尔遇到的“The request was aborted:Could not create SSL/TLS secure channel.”怎么解决?

    项目中涉及到调用第三方的Https的WebService,我使用的是原始的HttpWebRequest. 代码中已经考虑到是Https,加上了SSL3协议,加上了委托调用.但偶尔还是会碰到 The r ...

  8. [HTTPS] - 请求API失败(Could not create SSL/TLS secure channel)之解决

    背景 在单元测试中请求 HTTPS API 失败. 异常 Result StackTrace:  at System.Web.Services.Protocols.WebClientProtocol. ...

  9. The request was aborted: Could not create SSL/TLS secure channel

    一.背景: 公司底层服务CDN从Akamai迁移到阿里云之后, 使用该服务的一个应用报错如下: System.AggregateException: One or more errors occurr ...

随机推荐

  1. 为实施了IFD的Dynamics 365更换自签名的SSL证书以符合Chrome的要求

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复259或者20170704可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  2. Spring3 MVC DispatcherServlet 配置问题

    1. Spring MVC 是通过servlet来进行转发的,一般在web.xml中配置如下: <servlet> <servlet-name>dispatcher</s ...

  3. .NetCore~Json代替了Xml

    回到目录 在进行.netCore时代后,最大的变化就是对Json的使用更加主动,基本代替了之前的XML,像一些用户配置,系统配置,包包配置等都是基于json的,而web.config这个文件基本变成一 ...

  4. 关于SQL经典题

    最近刚刚练过的一道sql分享给大家, 先上题目: -- 部门表 CREATE TABLE DEPT( DEPTNO INT PRIMARY KEY, -- 部门编号 DNAME VARCHAR(14) ...

  5. Orchard 学习

    https://github.com/OrchardCMS/Orchard  源码下载 http://www.orchardch.com/  中文介绍网站

  6. nopcommerce 开源商城

    http://www.nopchina.net/  中文网 http://www.nopcommerce.com/downloads.aspx  源码下载  如果要在数据库中添加一个新的数据表,需要按 ...

  7. python函数(4):递归函数及二分查找算法

    人理解循环,神理解递归!  一.递归的定义 def story(): s = """ 从前有个山,山里有座庙,庙里老和尚讲故事, 讲的什么呢? ""& ...

  8. 关于canvas画布使用fillRect()时高度出现双倍效果解决办法

    当设置canvas的宽度和高度时,只有内嵌css有效,外部css会出现拉伸的情况,例如: <!DOCTYPE html> <html lang="en"> ...

  9. 关于bootstrap的一些想法

    老实说,作为一个前端人员,我不怎么会去用bootstrap,但是我会去看,会去了解. 首先,bootstrap其实是给后台以及前端新人用来快速完成一个页面的简单布局,不是按照设计稿psd格式给定做的那 ...

  10. 华为服务器Linux在线做RAID方法

    背景概述 最近维护大数据的一些主机,大概有3k+的数目,有很大一部分是华为的服务器,大部分是12块数据盘,单盘做RAID0来存放数据,但是通常硬件是不可靠的,磁盘损坏是常态, 然而磁盘损坏进行定位更换 ...