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 ...
随机推荐
- getResources提取资源文件
String pxsize = context.getResources().getString(R.string.hello); 资源文件格式: <?xml version="1.0 ...
- 集合用法笔记-Map用法
一.Map遍历 Map<String, String> map = new HashMap<String, String>(); map.put("1", ...
- .net 爬虫框架技术选型
个人认为爬虫框架分抓取框架和分析框架 1)抓取框架 .net 市面上好的似乎不多,选择要素分两种:1.轻量型,2.重量型. 1. 轻量型是可以定制一些特殊的功能或者插件开关形式.总体性能高,速度快. ...
- 怎么在linux Ubuntu上部署nodejs
今天特别开心,同时也有兴趣把最近的一些工作总结一下. 第一,方便记忆. 第二, 给需要的同学做参考 node.js 在本地的话,比较容易运行,node app.js 命令就搞定,但是当需要部署到生产环 ...
- linux系统编程之文件IO
1.打开文件的函数open,第一个参数表示文件路径名,第二个为打开标记,第三个为文件权限 代码: #include <sys/types.h> #include <sys/stat. ...
- Ambari安装之部署单节点集群
前期博客 大数据领域两大最主流集群管理工具Ambari和Cloudera Manger Ambari架构原理 Ambari安装之Ambari安装前准备(CentOS6.5)(一) Ambari安装之部 ...
- git创建版本库以及使用
Git使用教程(摘自tugenhua0707) 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央 ...
- 如何在Shell中快速切换目录
1.回到上一次进入的路经cd -2.回到Homecd ~3.自动补齐实例,cd /usr/src/redhat,可以用cd /u[TAB]s[TAB]r[TAB]4.!$ 表示上一个命令的最后一个参数 ...
- Java中的数值和集合
数组array和集合的区别: (1) 数值是大小固定的,同一数组只能存放一样的数据. (2) java集合可以存放不固定的一组数据 (3) 若程序事不知道究竟需要多少对象,需要在空间不足时自动扩增容量 ...
- Spring MVC的实现原理
Spring提供了DispatcherServlet,这个类不仅负责实现请求转发,还负责启动一个WebApplicationContext容器. 按照Spring一贯的IoC哲学,所有的Control ...