R
R
root092018-06-26 02:47:52
C++ / C#
root09, 2018-06-26 02:47:52

How to correctly put timeout in HttpWebRequest?

There is a method:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/536.36 (KHTML, like Gecko) Chrome/63.0.3239.142 Safari/536.36";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
                request.Headers.Add("Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6,zh;q=0.5,sv;q=0.4,zh-TW;q=0.3,es;q=0.2,de;q=0.1,nl;q=0.1");
                request.Timeout = 4000;
                request.ServicePoint.ConnectionLeaseTimeout = 4000;
                request.ServicePoint.MaxIdleTime = 4000;
                request.ReadWriteTimeout = 4000;
                request.ContinueTimeout = 4000;

                if (proxy.Length > 0)
                {
                    string[] prox = proxy.Split(':');
                    request.Proxy = new WebProxy(prox[0], Convert.ToInt32(prox[1]));
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }

Which receives the content of the page through a proxy, everything works well, except that there are such proxies that respond too long, and these timeouts do not help, the method can be executed much longer than these 4 seconds, you need to complete requests that take longer than 4 seconds, How can I do that?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question