2 hhdjsdnnm HHDJSDNNM 于 2017.09.14 21:36 提问

C# HttpWebRequest发送GET请求的问题

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();
 上面代码为什么在部分电脑上超时,手动打开网页没问题,求思路!





3个回答

showbo
showbo   Ds   Rxr 2017.09.15 08:40

添加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;
HHDJSDNNM
HHDJSDNNM 兄弟这个办法我在提问之前也做了测试,问题依旧
2 个月之前 回复
ouyangsixian
ouyangsixian   2017.09.15 10:06

超时有很多因素导致的,比如网络的原因,有时候网络顺畅就不会超时,网络不顺畅就超时。用一个Stopwatch 记录一下每一步所花的时间,看看是哪一段代码所花的时间较长。针对这段代码进行优化查看。

HHDJSDNNM
HHDJSDNNM 网络没问题,手动打开就很正常
2 个月之前 回复
HHDJSDNNM
HHDJSDNNM   2017.09.15 12:14

我刚才用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();
Csdn user default icon
上传中...
上传图片
插入图片