在访问网络的时候,我使用了webclient,为了使webclient带cookies我重写了相应的函数
protected override WebRequest GetWebRequest(Uri address)
{
//hwr = (HttpWebRequest)base.GetWebRequest(new Uri(url));
//如果是发送HTTPS请求
if (address.ToString().StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
hwr = WebRequest.Create(address) as HttpWebRequest;
//hwr.ProtocolVersion = HttpVersion.Version10;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
}
else
{
hwr = (HttpWebRequest)base.GetWebRequest(address);
}
//hwr.Method = "GET";
return hwr;
}
使用上面那种定法出错了,后来进过调才发现hwr的METHOD属性是GET,我在用webclient发送POST数据的时候也还是GET,所以就出错了。下面的写法是正确的,要使用父类的方法来自动得到对应的属性。下面的写法,得到的METHOD就是POST了。
protected override WebRequest GetWebRequest(Uri address)
{
//hwr = (HttpWebRequest)base.GetWebRequest(new Uri(url));
//如果是发送HTTPS请求
if (address.ToString().StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
hwr = (HttpWebRequest)base.GetWebRequest(address);
//hwr.ProtocolVersion = HttpVersion.Version10;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
}
else
{
hwr = (HttpWebRequest)base.GetWebRequest(address);
}
//hwr.Method = "GET";
return hwr;
}