Search code examples
resthttpwebrequestwebrequest

GetResponseAsync timeout after a few POST request


i'm making a POST request in C# like this:

var request = WebRequest.Create("http://xxx.xxx.xxx.xxx:8080/xxx/obj/data/xxx/commands/GET");
request.ContentType = "application/json";
((HttpWebRequest)request).Accept = "application/json";
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en");
NetworkCredential myNetworkCredentials = new NetworkCredential("myUser", "MyPass");
CredentialCache myCredentialCache = new CredentialCache
{
   { link, "Basic", myNetworkCredentials }
};
request.Credentials = myCredentialCache;
request.PreAuthenticate = true;
request.Method = "POST";
await request.GetResponseAsync();

When i'm using Postman to check the response, it's all ok, it works every time i send the request.

But programmatically, after 7-8 times, the await request.GetResponseAsync(); is giving me a Exception, "The Operation has timed out".

I don't know how to check this, in postman it's all ok, but in the app it failed after a few test. What can i do?


Solution

  • By default, you have only 10 Connection Limit when you start the App. So you need to setup this in the Uri for your request

    ServicePoint sp = ServicePointManager.FindServicePoint(uri);
    sp.ConnectionLimit = 1000;
    ServicePointManager.DefaultConnectionLimit = 1000;