The operation has timed out at System.Net.HttpWebRequest.GetResponse() while sending large number of requests...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am sending a large number of simultaneous requests to a particular web service with different data. To achieve this, I have created a number of threads(around 50 in number). Total number of requests per minute may increase up to 10000.
The application in the form of a windows service runs fine for a few minutes and then a operation time out error is encountered.



I have tried the usual suspects such as increasing DefaultConnectionLimit, closing the web response object. Since the requests do not take much time on server, I have also set the request Timeout and ReadWriteTimeout to 5 seconds.
Below is the code snippet which is called repeatedly by different threads.



// Below line is executed at the start of application
ServicePointManager.DefaultConnectionLimit = 15000;

// Below code is executed at repeatedly by different threads
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Host = hostName;
request.Proxy = null;
request.UserAgent = "Windows Service";
byte bytes = new byte[0];
if (body != null)
{
bytes = System.Text.Encoding.ASCII.GetBytes(body);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
}
request.Method = "POST";
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;

request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));

request.CookieContainer = this.cookieContainer;

if (body != null)
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}

HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
httpResponse.Close();









share|improve this question

























  • Are you using http 1.0 or 1.1? Try forcing to 1.0 : request.ProtocolVersion = HttpVersion.Version10; 1.1 is chunk mode and if you do not send next chunk message you will get a timout.

    – jdweng
    Feb 14 '18 at 11:13











  • @FaizanRabbani : -yes I tried to increase timeout but it just slows down subsequent requests and the error is logged after that timeout period. So, isn't really helping us.

    – pull420
    Feb 14 '18 at 11:20











  • @FaizanRabbani :- No, its a windows service.

    – pull420
    Feb 14 '18 at 11:23











  • Are you sure the problem is on your side? Perhaps the web service only (for example) services 3 requests at a time and queues the others?

    – mjwills
    Feb 14 '18 at 11:27






  • 1





    Can you try something this user suggested? stackoverflow.com/a/16744521/4222487

    – FaizanRabbani
    Feb 14 '18 at 11:32


















0















I am sending a large number of simultaneous requests to a particular web service with different data. To achieve this, I have created a number of threads(around 50 in number). Total number of requests per minute may increase up to 10000.
The application in the form of a windows service runs fine for a few minutes and then a operation time out error is encountered.



I have tried the usual suspects such as increasing DefaultConnectionLimit, closing the web response object. Since the requests do not take much time on server, I have also set the request Timeout and ReadWriteTimeout to 5 seconds.
Below is the code snippet which is called repeatedly by different threads.



// Below line is executed at the start of application
ServicePointManager.DefaultConnectionLimit = 15000;

// Below code is executed at repeatedly by different threads
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Host = hostName;
request.Proxy = null;
request.UserAgent = "Windows Service";
byte bytes = new byte[0];
if (body != null)
{
bytes = System.Text.Encoding.ASCII.GetBytes(body);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
}
request.Method = "POST";
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;

request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));

request.CookieContainer = this.cookieContainer;

if (body != null)
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}

HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
httpResponse.Close();









share|improve this question

























  • Are you using http 1.0 or 1.1? Try forcing to 1.0 : request.ProtocolVersion = HttpVersion.Version10; 1.1 is chunk mode and if you do not send next chunk message you will get a timout.

    – jdweng
    Feb 14 '18 at 11:13











  • @FaizanRabbani : -yes I tried to increase timeout but it just slows down subsequent requests and the error is logged after that timeout period. So, isn't really helping us.

    – pull420
    Feb 14 '18 at 11:20











  • @FaizanRabbani :- No, its a windows service.

    – pull420
    Feb 14 '18 at 11:23











  • Are you sure the problem is on your side? Perhaps the web service only (for example) services 3 requests at a time and queues the others?

    – mjwills
    Feb 14 '18 at 11:27






  • 1





    Can you try something this user suggested? stackoverflow.com/a/16744521/4222487

    – FaizanRabbani
    Feb 14 '18 at 11:32














0












0








0








I am sending a large number of simultaneous requests to a particular web service with different data. To achieve this, I have created a number of threads(around 50 in number). Total number of requests per minute may increase up to 10000.
The application in the form of a windows service runs fine for a few minutes and then a operation time out error is encountered.



I have tried the usual suspects such as increasing DefaultConnectionLimit, closing the web response object. Since the requests do not take much time on server, I have also set the request Timeout and ReadWriteTimeout to 5 seconds.
Below is the code snippet which is called repeatedly by different threads.



// Below line is executed at the start of application
ServicePointManager.DefaultConnectionLimit = 15000;

// Below code is executed at repeatedly by different threads
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Host = hostName;
request.Proxy = null;
request.UserAgent = "Windows Service";
byte bytes = new byte[0];
if (body != null)
{
bytes = System.Text.Encoding.ASCII.GetBytes(body);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
}
request.Method = "POST";
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;

request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));

request.CookieContainer = this.cookieContainer;

if (body != null)
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}

HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
httpResponse.Close();









share|improve this question
















I am sending a large number of simultaneous requests to a particular web service with different data. To achieve this, I have created a number of threads(around 50 in number). Total number of requests per minute may increase up to 10000.
The application in the form of a windows service runs fine for a few minutes and then a operation time out error is encountered.



I have tried the usual suspects such as increasing DefaultConnectionLimit, closing the web response object. Since the requests do not take much time on server, I have also set the request Timeout and ReadWriteTimeout to 5 seconds.
Below is the code snippet which is called repeatedly by different threads.



// Below line is executed at the start of application
ServicePointManager.DefaultConnectionLimit = 15000;

// Below code is executed at repeatedly by different threads
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Host = hostName;
request.Proxy = null;
request.UserAgent = "Windows Service";
byte bytes = new byte[0];
if (body != null)
{
bytes = System.Text.Encoding.ASCII.GetBytes(body);
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
}
request.Method = "POST";
request.Timeout = 5000;
request.ReadWriteTimeout = 5000;

request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));

request.CookieContainer = this.cookieContainer;

if (body != null)
{
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}

HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
httpResponse.Close();






c# timeout operation getresponse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 14 '18 at 11:28







pull420

















asked Feb 14 '18 at 11:07









pull420pull420

636




636













  • Are you using http 1.0 or 1.1? Try forcing to 1.0 : request.ProtocolVersion = HttpVersion.Version10; 1.1 is chunk mode and if you do not send next chunk message you will get a timout.

    – jdweng
    Feb 14 '18 at 11:13











  • @FaizanRabbani : -yes I tried to increase timeout but it just slows down subsequent requests and the error is logged after that timeout period. So, isn't really helping us.

    – pull420
    Feb 14 '18 at 11:20











  • @FaizanRabbani :- No, its a windows service.

    – pull420
    Feb 14 '18 at 11:23











  • Are you sure the problem is on your side? Perhaps the web service only (for example) services 3 requests at a time and queues the others?

    – mjwills
    Feb 14 '18 at 11:27






  • 1





    Can you try something this user suggested? stackoverflow.com/a/16744521/4222487

    – FaizanRabbani
    Feb 14 '18 at 11:32



















  • Are you using http 1.0 or 1.1? Try forcing to 1.0 : request.ProtocolVersion = HttpVersion.Version10; 1.1 is chunk mode and if you do not send next chunk message you will get a timout.

    – jdweng
    Feb 14 '18 at 11:13











  • @FaizanRabbani : -yes I tried to increase timeout but it just slows down subsequent requests and the error is logged after that timeout period. So, isn't really helping us.

    – pull420
    Feb 14 '18 at 11:20











  • @FaizanRabbani :- No, its a windows service.

    – pull420
    Feb 14 '18 at 11:23











  • Are you sure the problem is on your side? Perhaps the web service only (for example) services 3 requests at a time and queues the others?

    – mjwills
    Feb 14 '18 at 11:27






  • 1





    Can you try something this user suggested? stackoverflow.com/a/16744521/4222487

    – FaizanRabbani
    Feb 14 '18 at 11:32

















Are you using http 1.0 or 1.1? Try forcing to 1.0 : request.ProtocolVersion = HttpVersion.Version10; 1.1 is chunk mode and if you do not send next chunk message you will get a timout.

– jdweng
Feb 14 '18 at 11:13





Are you using http 1.0 or 1.1? Try forcing to 1.0 : request.ProtocolVersion = HttpVersion.Version10; 1.1 is chunk mode and if you do not send next chunk message you will get a timout.

– jdweng
Feb 14 '18 at 11:13













@FaizanRabbani : -yes I tried to increase timeout but it just slows down subsequent requests and the error is logged after that timeout period. So, isn't really helping us.

– pull420
Feb 14 '18 at 11:20





@FaizanRabbani : -yes I tried to increase timeout but it just slows down subsequent requests and the error is logged after that timeout period. So, isn't really helping us.

– pull420
Feb 14 '18 at 11:20













@FaizanRabbani :- No, its a windows service.

– pull420
Feb 14 '18 at 11:23





@FaizanRabbani :- No, its a windows service.

– pull420
Feb 14 '18 at 11:23













Are you sure the problem is on your side? Perhaps the web service only (for example) services 3 requests at a time and queues the others?

– mjwills
Feb 14 '18 at 11:27





Are you sure the problem is on your side? Perhaps the web service only (for example) services 3 requests at a time and queues the others?

– mjwills
Feb 14 '18 at 11:27




1




1





Can you try something this user suggested? stackoverflow.com/a/16744521/4222487

– FaizanRabbani
Feb 14 '18 at 11:32





Can you try something this user suggested? stackoverflow.com/a/16744521/4222487

– FaizanRabbani
Feb 14 '18 at 11:32












1 Answer
1






active

oldest

votes


















2














ServicePointManager.DefaultConnectionLimit limits the number of outgoing web requests to a given server. The default is generally 2 or 10.



If you are making 50 parallel calls to that web service, you should set ServicePointManager.DefaultConnectionLimit (at app startup) to a larger number (e.g. 40-50).



Additionally, you are not calling Close or Dispose on request. You should do this, or let using take care of it for you.






share|improve this answer


























  • So, when you make more simultaneous request than the DefaultConnectionLimit , aren't requests just queued? Could you lose a request this way?

    – Henk Holterman
    Mar 1 at 12:55











  • Yes they are queued @HenkHolterman. But if you queue enough of them, some will start to timeout (as the OP mentions). Imagine doing 10,000 requests a minute, and each request takes 10 seconds. If you do only 10 requests at a time (default value of this setting for a web app), clearly your throughput is effectively capped to 60 per minute. 9,940 of them will timeout.

    – mjwills
    Mar 1 at 12:58














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48785681%2fthe-operation-has-timed-out-at-system-net-httpwebrequest-getresponse-while-sen%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














ServicePointManager.DefaultConnectionLimit limits the number of outgoing web requests to a given server. The default is generally 2 or 10.



If you are making 50 parallel calls to that web service, you should set ServicePointManager.DefaultConnectionLimit (at app startup) to a larger number (e.g. 40-50).



Additionally, you are not calling Close or Dispose on request. You should do this, or let using take care of it for you.






share|improve this answer


























  • So, when you make more simultaneous request than the DefaultConnectionLimit , aren't requests just queued? Could you lose a request this way?

    – Henk Holterman
    Mar 1 at 12:55











  • Yes they are queued @HenkHolterman. But if you queue enough of them, some will start to timeout (as the OP mentions). Imagine doing 10,000 requests a minute, and each request takes 10 seconds. If you do only 10 requests at a time (default value of this setting for a web app), clearly your throughput is effectively capped to 60 per minute. 9,940 of them will timeout.

    – mjwills
    Mar 1 at 12:58


















2














ServicePointManager.DefaultConnectionLimit limits the number of outgoing web requests to a given server. The default is generally 2 or 10.



If you are making 50 parallel calls to that web service, you should set ServicePointManager.DefaultConnectionLimit (at app startup) to a larger number (e.g. 40-50).



Additionally, you are not calling Close or Dispose on request. You should do this, or let using take care of it for you.






share|improve this answer


























  • So, when you make more simultaneous request than the DefaultConnectionLimit , aren't requests just queued? Could you lose a request this way?

    – Henk Holterman
    Mar 1 at 12:55











  • Yes they are queued @HenkHolterman. But if you queue enough of them, some will start to timeout (as the OP mentions). Imagine doing 10,000 requests a minute, and each request takes 10 seconds. If you do only 10 requests at a time (default value of this setting for a web app), clearly your throughput is effectively capped to 60 per minute. 9,940 of them will timeout.

    – mjwills
    Mar 1 at 12:58
















2












2








2







ServicePointManager.DefaultConnectionLimit limits the number of outgoing web requests to a given server. The default is generally 2 or 10.



If you are making 50 parallel calls to that web service, you should set ServicePointManager.DefaultConnectionLimit (at app startup) to a larger number (e.g. 40-50).



Additionally, you are not calling Close or Dispose on request. You should do this, or let using take care of it for you.






share|improve this answer















ServicePointManager.DefaultConnectionLimit limits the number of outgoing web requests to a given server. The default is generally 2 or 10.



If you are making 50 parallel calls to that web service, you should set ServicePointManager.DefaultConnectionLimit (at app startup) to a larger number (e.g. 40-50).



Additionally, you are not calling Close or Dispose on request. You should do this, or let using take care of it for you.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 14 '18 at 11:34

























answered Feb 14 '18 at 11:20









mjwillsmjwills

16k52644




16k52644













  • So, when you make more simultaneous request than the DefaultConnectionLimit , aren't requests just queued? Could you lose a request this way?

    – Henk Holterman
    Mar 1 at 12:55











  • Yes they are queued @HenkHolterman. But if you queue enough of them, some will start to timeout (as the OP mentions). Imagine doing 10,000 requests a minute, and each request takes 10 seconds. If you do only 10 requests at a time (default value of this setting for a web app), clearly your throughput is effectively capped to 60 per minute. 9,940 of them will timeout.

    – mjwills
    Mar 1 at 12:58





















  • So, when you make more simultaneous request than the DefaultConnectionLimit , aren't requests just queued? Could you lose a request this way?

    – Henk Holterman
    Mar 1 at 12:55











  • Yes they are queued @HenkHolterman. But if you queue enough of them, some will start to timeout (as the OP mentions). Imagine doing 10,000 requests a minute, and each request takes 10 seconds. If you do only 10 requests at a time (default value of this setting for a web app), clearly your throughput is effectively capped to 60 per minute. 9,940 of them will timeout.

    – mjwills
    Mar 1 at 12:58



















So, when you make more simultaneous request than the DefaultConnectionLimit , aren't requests just queued? Could you lose a request this way?

– Henk Holterman
Mar 1 at 12:55





So, when you make more simultaneous request than the DefaultConnectionLimit , aren't requests just queued? Could you lose a request this way?

– Henk Holterman
Mar 1 at 12:55













Yes they are queued @HenkHolterman. But if you queue enough of them, some will start to timeout (as the OP mentions). Imagine doing 10,000 requests a minute, and each request takes 10 seconds. If you do only 10 requests at a time (default value of this setting for a web app), clearly your throughput is effectively capped to 60 per minute. 9,940 of them will timeout.

– mjwills
Mar 1 at 12:58







Yes they are queued @HenkHolterman. But if you queue enough of them, some will start to timeout (as the OP mentions). Imagine doing 10,000 requests a minute, and each request takes 10 seconds. If you do only 10 requests at a time (default value of this setting for a web app), clearly your throughput is effectively capped to 60 per minute. 9,940 of them will timeout.

– mjwills
Mar 1 at 12:58






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48785681%2fthe-operation-has-timed-out-at-system-net-httpwebrequest-getresponse-while-sen%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'