最佳答案
我试图找到一种方法来忽略证书检查时请求一个 Https 资源,到目前为止,我发现了一些有用的文章在互联网上。
但我仍然有一些问题。请检查我的代码。我只是不明白代码 ServicePointManager.ServerCertificateValidationCallback
是什么意思。
何时调用此委托方法?还有一个问题,我应该在哪里写代码?在 ServicePointManager.ServerCertificateValidationCallback
执行之前还是在 Stream stream = request.GetRequestStream()
执行之前?
public HttpWebRequest GetRequest()
{
CookieContainer cookieContainer = new CookieContainer();
// Create a request to the server
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_remoteUrl);
#region Set request parameters
request.Method = _context.Request.HttpMethod;
request.UserAgent = _context.Request.UserAgent;
request.KeepAlive = true;
request.CookieContainer = cookieContainer;
request.PreAuthenticate = true;
request.AllowAutoRedirect = false;
#endregion
// For POST, write the post data extracted from the incoming request
if (request.Method == "POST")
{
Stream clientStream = _context.Request.InputStream;
request.ContentType = _context.Request.ContentType;
request.ContentLength = clientStream.Length;
ServicePointManager.ServerCertificateValidationCallback = delegate(
Object obj, X509Certificate certificate, X509Chain chain,
SslPolicyErrors errors)
{
return (true);
};
Stream stream = request.GetRequestStream();
....
}
....
return request;
}
}