如何使用WebRequest连接SSL网站
答案:1 悬赏:80 手机版
解决时间 2021-03-02 04:25
- 提问者网友:你挡着我发光了
- 2021-03-01 18:29
如何使用WebRequest连接SSL网站
最佳答案
- 五星知识达人网友:春色三分
- 2021-03-01 18:36
//NET 1.0 /1.1 version:
//======================
using System.Net;
using System.Security.Cryptography.X509Certificates;
///
/// 使用WebRequest连接之前调用此方法就可以了.
///
private void MethodToAccessSSL()
{
//
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
// WebRequest myRequest = WebRequest.Create(url);
}
internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy() { }
public bool CheckValidationResult(ServicePoint sPoint,
X509Certificate cert, WebRequest wRequest,int certProb)
{
return true;
}
}
//NET 2.0 Version
//==============
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
///
/// 使用WebRequest连接之前调用此方法就可以了.
///
private void MethodToAccessSSL()
{
//
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate);
//WebRequest myRequest = WebRequest.Create(url);
}
// The following method is invoked by the RemoteCertificateValidationDelegate.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
//======================
using System.Net;
using System.Security.Cryptography.X509Certificates;
///
/// 使用WebRequest连接之前调用此方法就可以了.
///
private void MethodToAccessSSL()
{
//
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
// WebRequest myRequest = WebRequest.Create(url);
}
internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy() { }
public bool CheckValidationResult(ServicePoint sPoint,
X509Certificate cert, WebRequest wRequest,int certProb)
{
return true;
}
}
//NET 2.0 Version
//==============
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
///
/// 使用WebRequest连接之前调用此方法就可以了.
///
private void MethodToAccessSSL()
{
//
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate);
//WebRequest myRequest = WebRequest.Create(url);
}
// The following method is invoked by the RemoteCertificateValidationDelegate.
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
// Do not allow this client to communicate with unauthenticated servers.
return false;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯