Could not establish trust relationship for the SSL/TLS secure channel 问题解决方法
最近在写一个跟第三方对接的数据同步服务,在本地都没有问题,今天放到生产环境测试报错:
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 问题解决方法的更多相关文章
- 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 ...
- 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 ...
- https请求时出错:Could not establish trust relationship for the SSL/TLS secure channel
当我在用NET命名空间下获取URL的时候,提示如下错误: The underlying connection was closed: Could not establish trust relatio ...
- [bug]The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
写在前面 在模拟请求的时候,如果url为https的,会报这个错误.大概错误就是:基础连接已关闭:无法建立信任关系的SSL / TLS的安全通道. The underlying connection ...
- 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(若因为链接而导致不 ...
- 请求被中止: 未能创建 SSL/TLS 安全通道,以及解决方法,即:Could not create SSL/TLS secure channel
C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel) 以及 X509Certificate2 temp ...
- 偶尔遇到的“The request was aborted:Could not create SSL/TLS secure channel.”怎么解决?
项目中涉及到调用第三方的Https的WebService,我使用的是原始的HttpWebRequest. 代码中已经考虑到是Https,加上了SSL3协议,加上了委托调用.但偶尔还是会碰到 The r ...
- [HTTPS] - 请求API失败(Could not create SSL/TLS secure channel)之解决
背景 在单元测试中请求 HTTPS API 失败. 异常 Result StackTrace: at System.Web.Services.Protocols.WebClientProtocol. ...
- The request was aborted: Could not create SSL/TLS secure channel
一.背景: 公司底层服务CDN从Akamai迁移到阿里云之后, 使用该服务的一个应用报错如下: System.AggregateException: One or more errors occurr ...
随机推荐
- mysql常见的优化方法
1.选取适当的字段属性.例如,在定义邮政编码这个字段时,如果将其设置为CHAR(255),显然给数据库增加了不必要的空间,甚至使用VARCHAR这种类型也是多余的,因为CHAR(6)就可以很好的完成任 ...
- [钉钉通知系列]Jenkins发布后自动通知
一.前言 最近使用Jenkins进行自动化部署,但是发布署后,并没有相应的通知,虽然有邮件发送通知,但是发现邮件会受限于大家接受的设置,导致不能及时看到相关的发布内容.由于之前有用Gitlab推送消息 ...
- Java 接口-抽象类解析
对于面向对象编程,抽象是它的三大特征(抽象.继承.多态)之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类. 这两者既相似又存异.诸位在初学的时候也会傻傻分不清接口与抽象类的区别,大 ...
- Android自动问题——黑屏、死机等解决方法
今天用了下Android Studio,出现了一些问题,现在将启动过程中遇到的问题和解决方案列出来,方便大家参考. 安装过程不多说,网上一搜一大把. 那直接说问题吧: 1. 无法启动,报错:Faile ...
- (转)CentOS6.5下Redis安装与配置
场景:项目开发中需要用到redis,之前自己对于缓存这块一直不是很理解,所以一直有从头做起的想法. 本文详细介绍redis单机单实例安装与配置,服务及开机自启动.如有不对的地方,欢迎大家拍砖o(∩_∩ ...
- MongoDB数据库的数据类型和$type操作符
前面的话 本文将详细介绍MongoDB数据库的数据类型和$type操作符 数据类型 MongoDB支持以下数据类型 类型 数字 备注 Double 1 双精度浮点数 - 此类型用于存储浮点值 Stri ...
- 43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...
- 刚装的系统C盘占空间特别大怎么办?关闭win7的系统还原和调整虚拟内存
刚装的win7系统,C盘占空间特别大,清理垃圾,清理碎片之后还是特别大,检查一下,系统才20G,C盘却占用了40G,其实是没有关闭系统还原,再就是没有调整虚拟内存.win7旗舰版(其他版本也可能这样, ...
- C++ STL vector详解
一.解释: vector(向量):是一种顺序容器,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界.而vector正好弥补了这个缺陷,它 ...
- docker安装方法(常见安装出错问题汇总)
参考资料: 1. 开源中国 http://www.oschina.net/translate/nstalling-dockerio-on-centos-64-64-bit?cmp Docker 是一 ...