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; //总是接受
}
http://www.cnblogs.com/ccsharp/p/3270344.html
去掉ServicePointManager相关代码,和http一样请求,只是将请求地址由http改为https
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();
}
}
改成这样试过了。也是不行!
@ansonpan: 出现什么错误提示?
@dudu: 无法连接到远程服务器
@ansonpan: 那还是使用ServicePointManager,试试 ServicePointManager.SecurityProtocol 的其他枚举值:SecurityProtocolType.Tls, SecurityProtocolType.Tls11, SecurityProtocolType.Tls12
@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);
@dudu: 用SoapUI 调试对方的接口,又是通的,可以返回报文的!
@ansonpan: 用curl命令可以成功调用吗?
服务器,操作系统是2003吗
http模块已经封装了,不然http这个模块还有多少意义。就如楼上所言http: -> https:即可
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
加上这句
ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
这个有效
我这边遇到的情况是,我们程序调用第三方的接口,然后超时后会提示这个错误,在调用第三方接口的过程时,使用try...catch,然后就可以解决了,以上,仅供参考