【问题标题】:HttpWebRequest - Request TerminationHttpWebRequest - 请求终止
【发布时间】:2017-02-04 15:07:02
【问题描述】:

对不起,我的英语不好。我每 10 秒使用一次 HttpWebRequest。但有时,响应是等待很长时间。 10秒后,下一个过程来了。因此,请求被堆叠起来。由于公司在总部采取的措施,我被禁止了,因为之前的连接还没有关闭。我设置了请求的属性,如 Timeout、ContinueTimeout、ReadWriteTimeout,但结果没有改变。实际上,它属于异常。但是当我观看“Charles Proxy”时,请求仍在等待响应。但我必须以某种方式阻止它。我分享我的代码和查尔斯的照片。谢谢你的回答。最好的问候。

        string jsondata = "";
        const string url = "https://website.com";

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        NameValueCollection header_items = GetHeaders();
        req.Headers.Add(header_items);
        req.Method = "POST";
        req.Host = "website.com";
        req.Accept = "application/json, text/plain, */*";
        req.ContentType = "application/json;charset=utf-8";
        req.KeepAlive = false;
        req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)";
        req.Referer = "https://website.com";
        req.AllowAutoRedirect = false;
        req.ContinueTimeout = 5000;
        req.ReadWriteTimeout = 5000;
        req.Timeout = 5000;
        req.ContentLength = 0;
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
        ServicePointManager.DefaultConnectionLimit = 500;
        ServicePointManager.DnsRefreshTimeout = 0;
        ServicePointManager.MaxServicePointIdleTime = 5000;
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    jsondata = reader.ReadToEnd();
                    reader.Close();
                }
                response.Close();
            }
            if (jsondata.Length > 200)
            {
                //Do Something
            }
        }
        catch (WebException ex)
        {
            req.Abort();
            //Do Something
        }
        catch (Exception ex)
        {
            req.Abort();
            //Do Something
        }

【问题讨论】:

    标签: c# asp.net httpwebrequest httpwebresponse connection-timeout


    【解决方案1】:

    尝试为您的请求设置代理设置。多次查找代理需要太登录HttpWebRequest

    req.Proxy = GetSystemWebProxy();
    

    也看看这个:https://en.code-bude.net/2013/01/21/3-things-you-should-know-to-speed-up-httpwebrequest/

    【讨论】:

    • 我已经学习并应用了您给出的主题,但没有任何改变。
    猜你喜欢
    • 2011-01-28
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多