A
A
ATauenis2019-12-25 13:37:50
.NET
ATauenis, 2019-12-25 13:37:50

How to overcome timeouts out of the blue in WebClient?

All the best% time of day%! There is a program that regularly downloads files over HTTPS. It often happens that 20-30 pieces at once (small volume). Periodically, part of the requests hangs, and then falls off on a timeout. I noticed that most often freezes + timeouts occur in groups of requests that started at the wrong time +/- 10 seconds. If you restart the program, the network comes to life for it. The same thing if you wait a couple of minutes, and let all requests time out. Then everything works fine for a while, until the glitch repeats.

What are the options for why this is happening? From browsers, Skype, other network software, there are no problems, the network is stable. Servers also always respond from other software.

Calls are made in two ways.
1st:

WebClient WC = new WebClient(); //not HttpClient because it's appear in .net 4.0
Exception WCerr = null;

WC.OpenReadAsync(new Uri(SrcUrl)); //not OpenReadTaskAsync for compatibility with WinXP
WC.OpenReadCompleted += (object sender, OpenReadCompletedEventArgs e) =>
{
if (e.Error != null) { WCerr = e.Error; return; }
if (ConvStdin is MemoryStream) ConvStdin = e.Result;
};
while (ConvStdin is MemoryStream && WCerr == null) { /*wait*/ }
if (WCerr != null) throw WCerr.InnerException ?? WCerr; //вот тут вылезает Timeout


2nd:
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Host);
AddHeaders(Headers, webRequest); //моя функция, тут всё нормально
webRequest.Method = Method;
webRequest.AllowAutoRedirect = AllowAutoRedirect;
webRequest.CookieContainer = CC;
webRequest.ProtocolVersion = HttpVersion.Version11;
webRequest.KeepAlive = true;
webRequest.ServicePoint.Expect100Continue = false;

webResponse = (HttpWebResponse)webRequest.GetResponse(); //иногда лезет WebException, Timeout


Both zadolbali vykabluchivaniya.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2019-12-25
@ATauenis

You probably know that there is a limit on the number of connections to one host. The default is 2 connections for normal applications and 10 for applications running as ASP.NET hosts.
ServicePointManager.DefaultConnectionLimit

A
Alexander Yudakov, 2019-12-25
@AlexanderYudakov

When the HttpWebResponse is finished processing, be sure to call Dispose(), for example, like this:

using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
{
    // обработка ответа
}

And to lift up to heaven ServicePoint.ConnectionLimitis a crutch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question