How do I specify a global HttpWebRequest timeout? - c#

I'm using a twitter library that uses HttpWebRequest internally to make requests to the twitter API. For some odd reason, the requests sometimes take a lot of time to complete (~10 minutes).
The HttpWebRequest object is not exposed by the library.
Is there any way to specify a global timeout and readwritetimeout for the requests, perhaps via app.config?

Unfortunately not currently possible. The constructor of HttpWebRequest has this value hardcoded - reference source.

That timeout is in milliseconds - so 2000ms = only 2 seconds.
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("URL");
req.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["timeOut"]);
Req.ReadWriteTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["readWriteTimeout "]);
App.config
<appSettings>
<add key="timeOut" value="200" />
<add key="readWriteTimeout " value="10000" />
</appSettings>
Timeout = time spent trying to establish a connection (not including lookup time)
ReadWriteTimeout = time spent trying to read or write data after connection established

Related

Need help setting timeout for http request

I want to set the timeout value to 5 mins for the http client request call. So I am getting the value for timeout in my app.config as follows and using it to set the client request for 5 mins timeout session. But I am getting error as mentioned below. How do I fix this?
<add key="ClientSrvcTimeout" value="300000" />
I am using the above to set the http client timeout.
client.Timeout = Convert.ToInt32(ConfigurationManager.AppSettings["ClientSrvcTimeout"]);
But I am getting error as "Cannot implicitly convert type string to System.Timespan".
How do I fix this?
The client.Timeout is a TimeSpan and your app setting is an integer serialized to a string. And the integer represents milliseconds.
int milliseconds = int.Parse(ConfigurationManager.AppSettings["ClientSrvcTimeout"]);
client.Timeout = TimeSpan.FromMilliseconds(milliseconds);
I would suggest renaming the app settting to reflect that it is milliseconds, e.g. "ClientSrvcTimeoutMilliseconds".

Issues with C# HttpClient Timeout

I'm making a simple GetAsync() request with an HttpClient object. When I retrieve a small amount of data this way, the request works fine. When I try to retrieve a large amount of data the request times out.
I tried to set the HttpClient's Timeout property to 5 minutes like so:
var client = new HttpClient();
client.Timeout = new TimeSpan(0, 5, 0);
before making the call, but it still times out after ~60 seconds.
I've read in several answers to use HttpWebRequest to access more granular timeout options, but I'm using .net 4.5.2 and references to HttpWebRequest won't compile.
How can I increase the readwrite timeout of the HttpClient?

Timeout settings seem to have no effect

I am trying to set a timeout for a special request which will take a long time to process. Because of this, I am trying to set the timeout, like this:
client.RequestFilter = r => {
r.Timeout = 1000000;
r.ReadWriteTimeout = 1000000;
}
However, these settings seem to have no effect; the request still times out in about 30 seconds. Is there some hack I can use to set the timeout properly ?
ETA: The response I'm receiving is a stream; I do it like this:
var stream = client.Send<Stream>(requestDto);
Is there a better way ?
ServiceStack's Service Clients is just a wrapper around HttpWebRequest so your code ends up setting the HttpWebRequest Timeout and ReadWriteTimeout properties directly.
The Request Filter gives you direct access to the HttpWebRequest instance used and setting the Timeout properties should work as expected. Other than that the only class that can modify behavior of .NET's HttpWebRequest is System.Net.ServicePointManager which lets you configure some properties like DefaultConnectionLimit and DnsRefreshTimeout, etc. But there's no additional Request Timeout properties.
The alternative solution you can try is to use ServiceStack's JsonHttpClient which as it's built on Microsoft's newer HttpClient library, you may have better luck with it. Although it's recommended to use the Async API's since the Sync API's are just blocking on the HttpClient's underlying Async API's.
For the API call itself, you should access the stream in a using block, e.g:
using (var stream = client.Send<Stream>(requestDto))
{
}

Timeout error in ASP.NET mvc

I am facing a weird issue, I am running Quartz jobs to fetch data from 2 different URL, but getting timeout error on one, another is working just fine. The inner exception of error that I get says 'The operation has timed out'.
One more interesting thing is that I am not getting this error on my local system, on my local, both the jobs are fetching data correctly, but on server one fails.
I also spoke to the team of the website from which I am fetching data, they told me that their configuration for both urls are same, so there is no issue at their end.
I have made some changes in my webconfig file while trying to fix this issue by reading online, but no luck yet.
I have added this line in the appSettings tag in web.config
<add key="SqlCommandTimeOut" value="10000000" />
My connection string looks like:
<add name="xyzDBEntities" connectionString="metadata=res://*/xyzDB.csdl|res://*/xyzDBEntities.ssdl|res://*/xyzDBEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=SQL1234.xyzsite.com;initial catalog=xyzDB;User Id=xyzDB_admin;Password=xyzpassword;App=EntityFramework"" providerName="System.Data.EntityClient" />
It would be great if someone can help me in sorting this out. Please let me know if more details are needed. Thanks!
Extending WebClient class and overriding GetWebRequest() by setting timeout to 1 minute resolved my issue.
public class CustomWebClient : WebClient{
protected override WebRequest GetWebRequest(Uri address){
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.Timeout = 60000; //1 minute timeout
return request;
}
}

WebClient.DownloadString() + REST + HTTPS + Win2003 = hung thread

I've been battling with this issue for over 2 weeks now and have gotten nowhere.
First the environment: Windows 2003 R2 SP2 / SharePoint 2007 SP1 / .NET 3.5.
Basically, we are making web service calls to gather data from a remote API. The API has several endpoints for REST and several for SOAP. The endpoints are HTTPS endpoints with digest authentication. When we make calls with the SOAP endpoints, everything seems to work just fine. But then we try to make a call using REST and the thread hangs then dies a horrible death when IIS decides that the thread isn't responding anymore and kills it. At first, we thought this was an SSL issue (and it still might be) because we don't see any issues when using the HTTP endpoints (route to the same API just not SSL).
Below is the code we're using to make the REST call:
private void Process(HttpContext context, String url, String restParam)
{
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(validateCertificate);
WriteLogMessage("Start Process");
String pattern = "{0}{1}";
String address = String.Format(pattern, url, restParam);
WriteLogMessage("ADDRESS is" + address);
LSWebClient client = new LSWebClient();
client.Timeout = 600000;
WriteLogMessage("TIMEOUT (client.Timeout) is " + client.Timeout.ToString());
client.Credentials = new NetworkCredential(XYZConfigurationSettings.APIUserName, XYZConfigurationSettings.APIPassword);
try {
String result = client.DownloadString(address);
WriteLogMessage("End Process. RESULT length is " + (result != null ? result.Length : 0));
context.Response.Write(result);
}
catch (Exception ex)
{
WriteLogMessage("EXCEPTION!!! Message----" + ex.Message + "---- StackTrace ----" + ex.StackTrace + "");
}
}
private bool validateCertificate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
{
WriteLogMessage("bypassAllCertificateStuff");
return true;
}
So, crappy code aside, we put in a few things here to try to get around what we thought was an SSL Certificate issue. (setting the request timeout to 10 minutes, using custom certificate validation, etc...) However, none of this seems to fix the issue.
Here's the result of our logging:
2/28/2011 3:35:28 PM: Start
2/28/2011 3:35:28 PM: Start Process
2/28/2011 3:35:28 PM: ADDRESS ishttps://<host>/ws/rs/v1/taxonomy/TA/root/
2/28/2011 3:35:28 PM: TIMEOUT (client.Timeout) is 600000
2/28/2011 3:35:50 PM: CheckValidationResult
2/28/2011 3:35:50 PM: bypassAllCertificateStuff
2/28/2011 3:41:51 PM: EXCEPTION!!! Message ----Thread was being aborted.---- StackTrace ---- at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)
at System.Net.Connection.CompleteStartRequest(Boolean onSubmitThread, HttpWebRequest request, TriState needReConnect)
at System.Net.Connection.SubmitRequest(HttpWebRequest request)
at System.Net.ServicePoint.SubmitRequest(HttpWebRequest request, String connName)
at System.Net.HttpWebRequest.SubmitRequest(ServicePoint servicePoint)
at System.Net.HttpWebRequest.GetResponse()
at System.Net.WebClient.GetWebResponse(WebRequest request)
at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
at XYZ.DAO.Handlers.RestServiceHandler.Process(HttpContext context, String url, String restParam)
at XYZ.DAO.Handlers.RestServiceHandler.ProcessRequest(HttpContext context)----
I have attempted to use my browser to view the return data, but the browser is IE6, which doesn't support SSL. However, I can see (in Fiddler / Charles proxy) that it does attempt to make the request and receives a 401 error but since I can not see server traffic using these programs I can not tell at exactly what step the error is happening.
To make matters worse, I can not reproduce this issue on any other server I have (note: they are all Windows 2008 servers).
So, in summary, here's what I've found:
SOAP - work
REST - no work
Win2008 - work
Win2003 - no work
HTTP - work
HTTPS - no work
If anyone has any insight or any other debugging / information gathering that I haven't tried I would be extremely greatful.
You should be able to get a bunch more tracing information if you add the following to your client .config file.
<system.diagnostics>
<sources>
<source name="System.Net" switchValue="Information, ActivityTracing">
<listeners>
<add name="System.Net"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="System.Net.trace.log" />
</listeners>
</source>
</sources>
</system.diagnostics>
I've found what was causing the web service call to hang - the issue was that the service we were calling was using replay attack protection along with digest security:
Our server would send an initial
request sans security header
The request was responded to with a
standard 401 challenge providing a
nonce for use. (That nonce expires
after 10 seconds after the
challenge)
Our server then took 30 seconds to
generate a second response using
this nonce
So the remote server would then find the
expired nonce and again issue
another 401 challenge.
The cycle would continue until the local server's thread was terminated. However, why our local server is taking 30 $##%! seconds to generate a security header is beyond me. I inspected the logs that were provided through the diagnostics above, but none of it was much help. I'm going to chalk it up to the server being overloaded and not having enough memory to process it's way out of a wet paper bag.

Categories