POST数据到HTTPS站点的时候需要设置ServicePointManager类的ServerCertificateValidationCallback属性,并且在POST到https://passport.baidu.com/?login时还需要将HttpWebResquest实例的ProtocolVersion属性设置为HttpVersion.Version10(这个未验证是否所有的HTTPS站点都需要设置),否则在调用GetResponse()方法时会抛出“基础连接已经关闭: 连接被意外关闭。”的异常。
1
2
3
4
5
6
7
8
9
10
11
|
//如果是发送HTTPS请求 解决基础连接已经关闭: 连接被意外关闭
if
(url.StartsWith( "https" , StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult;
req.ProtocolVersion = HttpVersion.Version10;
}
private
static
bool
CheckValidationResult( object
sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ return
true ; //总是接受
} |