ServicePointManager.DefaultConnectionLimit = 500;
ServicePointManager.Expect100Continue = true;
ServicePointManager.CheckCertificateRevocationList = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
string url = @"https://login.wx2.qq.com/jslogin?appid=wx782c26e4c19acffb";
HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url);
wRequest.Method = "GET";
wRequest.ContentType = "text/javascript";
wRequest.CookieContainer = CookiesContainer; //启用cookie
if (CookiesContainer == null)
{
CookiesContainer = new CookieContainer();
}
WebResponse wResponse = wRequest.GetResponse();
Stream stream = wResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
string str = reader.ReadToEnd(); //url返回的值
MessageBox.Show(str);
reader.Close();
wResponse.Close();
上面代码为什么在部分电脑上超时,手动打开网页没问题,求思路!
添加using System.Security.Cryptography.X509Certificates;空间,然后添加下面的代码
public static bool SecurityValidate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;//直接确认,不然打不开,会出现超时错误
}
////////////添加这句
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(SecurityValidate);
//////////////
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
超时有很多因素导致的,比如网络的原因,有时候网络顺畅就不会超时,网络不顺畅就超时。用一个Stopwatch 记录一下每一步所花的时间,看看是哪一段代码所花的时间较长。针对这段代码进行优化查看。
我刚才用VB6的INET控件访问,在故障电脑上也很正常,代码修改后问题依旧,而且概率不低,我这边测试了8台电脑,不同的网络环境,有4台电脑异常!
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(SecurityValidate);
//////////////
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.DefaultConnectionLimit = 500;
ServicePointManager.Expect100Continue = true;
ServicePointManager.CheckCertificateRevocationList = false;
System.Net.ServicePointManager.Expect100Continue = false;
string url = @"https://login.wx2.qq.com/jslogin?appid=wx782c26e4c19acffb";
HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url);
wRequest.KeepAlive =true;
wRequest.ProtocolVersion = HttpVersion.Version10;
wRequest.Method = "GET";
wRequest.ContentType = "text/javascript";
wRequest.CookieContainer = CookiesContainer; //启用cookie
if (CookiesContainer == null)
{
CookiesContainer = new CookieContainer();
}
WebResponse wResponse = wRequest.GetResponse();
Stream stream = wResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
string str = reader.ReadToEnd(); //url返回的值
MessageBox.Show(str);
reader.Close();
wResponse.Close();