C# 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。
2015-12-21 00:42
711 查看
今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,但是程序不是到服务器以后一直提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系"。
后来把DLL文件进行反编译,发现是在获得请求的时候出错了。
引用
WebResponse response = WebRequest.Create("https://……").GetResponse();
定义一个类,来对远程X.509证书的验证,进行处理,返回为true.我们要自己定义一个类,然后在客户单调用WCF服务之前,执行一次即可。代码如下:
public static class Util
{
/// <summary>
/// Sets the cert policy.
/// </summary>
public static void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback
+= RemoteCertificateValidate;
}
/// <summary>
/// Remotes the certificate validate.
/// </summary>
private static bool RemoteCertificateValidate(
object sender, X509Certificate cert,
X509Chain chain, SslPolicyErrors error)
{
// trust any certificate!!!
System.Console.WriteLine("Warning, trust any certificate");
return true;
}
}
你要在WebRequest.Create("https://……").GetResponse();调用操作点前先调用这个方法: Util.SetCertificatePolicy();
后来把DLL文件进行反编译,发现是在获得请求的时候出错了。
引用
WebResponse response = WebRequest.Create("https://……").GetResponse();
定义一个类,来对远程X.509证书的验证,进行处理,返回为true.我们要自己定义一个类,然后在客户单调用WCF服务之前,执行一次即可。代码如下:
public static class Util
{
/// <summary>
/// Sets the cert policy.
/// </summary>
public static void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback
+= RemoteCertificateValidate;
}
/// <summary>
/// Remotes the certificate validate.
/// </summary>
private static bool RemoteCertificateValidate(
object sender, X509Certificate cert,
X509Chain chain, SslPolicyErrors error)
{
// trust any certificate!!!
System.Console.WriteLine("Warning, trust any certificate");
return true;
}
}
你要在WebRequest.Create("https://……").GetResponse();调用操作点前先调用这个方法: Util.SetCertificatePolicy();
相关文章推荐
- 将Unicon字符串转成汉字String C#
- C# 过滤空格、换行、script、style等等标签
- 分享一个C#编写简单的聊天程序(详细介绍)
- C#:使用MD5对用户密码加密与解密
- C#与excel操作
- 【C#学习】——集合和数组
- C# 实现可克隆(ICloneable)的类型
- C#用开源软件组合可用的软件
- c#编写部署windows服务
- c# 内存的具体表现- 通用类型系统 深拷贝 浅拷贝 函数传参
- C#基础知识点总结
- 【转载】C#时间差的计算,精确输出“年月天时分秒”
- csharp正则表达式的使用
- c#知识点整理
- 设计模式C#实现(十三)——享元模式(蝇量模式)
- C#小例子(2)——方法
- C# 中的取整
- C#字符串处理之大小写转换
- C# Movie Player
- C#——重写与抽象类