首页 新闻 会员 周边 捐助

请求被中止: 未能创建 SSL/TLS 安全通道! 搞了3天,一直没解决!

0
悬赏园豆:100 [待解决问题]

HttpWebRequest 请求第三方https接口,一直提示未能创建 SSL/TLS 安全通道! 证书验证创建委托一直返回true 也不行。

 

 public static string Invoke_Show(string YesOrNoEntry, string MaxReturnedRows)
    {
     
          string url = ConfigurationManager.AppSettings["WPUR"].ToString();      
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "text/xml; charset=utf-8";
        request.Method = "POST";

        using (Stream requestStream = request.GetRequestStream())
        {
            byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
            requestStream.Write(paramBytes, 0, paramBytes.Length);
        }
        Stream reqstream = request.GetRequestStream();      
        request.Headers.Set("Pragma", "no-cache");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();       
        Stream streamReceive = response.GetResponseStream();
        Encoding encoding = Encoding.UTF8;
        StreamReader streamReader = new StreamReader(streamReceive, encoding);
        string strResult = streamReader.ReadToEnd();
        streamReceive.Dispose();
        streamReader.Dispose();
        return strResult;  
    }

            //Implement the ICertificatePolicy interface.
    public class CertPolicy : ICertificatePolicy
    {
        public bool CheckValidationResult(ServicePoint srvPoint,
        X509Certificate certificate, WebRequest request, int certificateProblem)
        {           
            return true;
        }
    }

    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true; //总是接受  
    }

ansonpan的主页 ansonpan | 初学一级 | 园豆:82
提问于:2017-09-28 09:10
< >
分享
所有回答(7)
0

http://www.cnblogs.com/ccsharp/p/3270344.html

Daniel Cai | 园豆:10424 (专家六级) | 2017-09-28 09:18
0

去掉ServicePointManager相关代码,和http一样请求,只是将请求地址由http改为https

dudu | 园豆:29570 (高人七级) | 2017-09-28 09:39

public static string CreatePostHttpResponse(string url, string datastr)
{

HttpWebRequest request = null;
request = WebRequest.Create(url) as HttpWebRequest;
// request.ProtocolVersion = HttpVersion.Version11;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";

 using (Stream requestStream = request.GetRequestStream())

{
byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
requestStream.Write(paramBytes, 0, paramBytes.Length);
}
WebResponse webResponse = request.GetResponse();

using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
string result = "";
return result = myStreamReader.ReadToEnd();

}

改成这样试过了。也是不行!

支持(0) 反对(0) ansonpan | 园豆:82 (初学一级) | 2017-09-28 10:04

@ansonpan: 出现什么错误提示?

支持(0) 反对(0) dudu | 园豆:29570 (高人七级) | 2017-09-28 10:06

@dudu: 无法连接到远程服务器

支持(0) 反对(0) ansonpan | 园豆:82 (初学一级) | 2017-09-28 10:16

@ansonpan: 那还是使用ServicePointManager,试试 ServicePointManager.SecurityProtocol 的其他枚举值:SecurityProtocolType.Tls, SecurityProtocolType.Tls11, SecurityProtocolType.Tls12

支持(0) 反对(0) dudu | 园豆:29570 (高人七级) | 2017-09-28 10:22

@dudu: 都试了,我的是.net 3.5 ,是了Tls 和SSL3 都不行!

ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertificateValidate);
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls ;

HttpWebRequest request = null;
request = WebRequest.Create(url) as HttpWebRequest;
// request.ProtocolVersion = HttpVersion.Version11;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.Proxy = null;

request.Credentials = System.Net.CredentialCache.DefaultCredentials;
//X509Certificate cer = new X509Certificate("D:\\wowprime.cer");
//request.ClientCertificates.Add(cer);

// X509Certificate Cert = X509Certificate.CreateFromCertFile(HttpContext.Current.Server.MapPath("/wowprime.cer"));
// Handle any certificate errors on the certificate from the server.

// request.ClientCertificates.Add(X509Certificate.CreateFromCertFile(HttpContext.Current.Server.MapPath("/wowprime.cer")));
//// request.ClientCertificates.Add(X509Certificate.CreateFromCertFile(HttpContext.Current.Server.MapPath("/cacerts.cer")));
//ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
////打开本地计算机下的个人证书存储区
//X509Store certStore = new System.Security.Cryptography.X509Certificates.X509Store(StoreName.My, StoreLocation.LocalMachine);
//certStore.Open(OpenFlags.ReadOnly);
////根据名称查找匹配的证书集合,这里注意最后一个参数,传true的话会找不到
//X509Certificate2Collection certCollection = certStore.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, "WPSHAV03", false);
//int index = certCollection.Count;
////将证书添加至客户端证书集合
//request.ClientCertificates.Add(certCollection[0]);

//X509Certificate2 tempSignCert = new X509Certificate2(HttpContext.Current.Server.MapPath("/wowprime.cer"), "123456");
//request.ClientCertificates.Add(tempSignCert);
//System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate X509certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
//{
// return true;
//};

//X509Certificate2 certificate = new X509Certificate2(Properties.Resources.client, Properties.Resources.httpsKey8843);
//request.ClientCertificates.Add(certificate);

支持(0) 反对(0) ansonpan | 园豆:82 (初学一级) | 2017-09-28 10:26

@dudu: 用SoapUI 调试对方的接口,又是通的,可以返回报文的!

支持(0) 反对(0) ansonpan | 园豆:82 (初学一级) | 2017-09-28 10:41

@ansonpan: 用curl命令可以成功调用吗?

支持(0) 反对(0) dudu | 园豆:29570 (高人七级) | 2017-09-28 11:12
0

服务器,操作系统是2003吗

万法归一 | 园豆:257 (菜鸟二级) | 2017-09-28 13:44
0

http模块已经封装了,不然http这个模块还有多少意义。就如楼上所言http:  -> https:即可

花飘水流兮 | 园豆:13617 (专家六级) | 2017-09-29 09:42
0

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

加上这句

夜色、花清浅 | 园豆:202 (菜鸟二级) | 2017-10-11 09:01
0

ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

Giant150 | 园豆:1172 (小虾三级) | 2017-12-15 16:55

这个有效

支持(0) 反对(0) zeje | 园豆:204 (菜鸟二级) | 2018-05-15 17:38
0

我这边遇到的情况是,我们程序调用第三方的接口,然后超时后会提示这个错误,在调用第三方接口的过程时,使用try...catch,然后就可以解决了,以上,仅供参考

喂喂喂~辉夳狼~ | 园豆:202 (菜鸟二级) | 2019-05-07 11:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册