13

I am trying to make a web service request call to a third part web site who's server is a little unreliable. Is there a way I can set a timeout on a request to this site? Something like this pseudo code:

try // for 1 minute
{
    // Make web request here
    using (WebClient client new WebClient()) //...etc.
}
catch
{
}

5 Answers 5

29

You could use the Timeout property:

var request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Timeout = 1000; //Timeout after 1000 ms
using (var stream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream))
{
    Console.WriteLine(reader.ReadToEnd());
}

UPDATE:

To answer the question in the comment section about XElement.Load(uri) you could do the following:

var request = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com/feeds");
request.Timeout = 1000; //Timeout after 1000 ms
using (var stream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream))
{
    var xel = XElement.Load(reader);
}
Sign up to request clarification or add additional context in comments.

6 Comments

I'll try this out and let you know how it works. Can I put this all in a try catch statement so that I can return an error message if it times out?
Of course, putting try/catch is what you need to handle the exception and return a proper message.
This may be for another question, but do you know how I can also use this with Xelement.Load(uri)?
@victor_foster seeing as how this is tagged asp.net-mvc. Whilst you can set the timeout on the webrequest IIS might still cut the request that initiated the action of. Don't forget to set the timeout on the current HTTP request (that invoked your action) too. In debug mode the default timeout is a year but in a release build IIS will default to 90 seconds.
@Darin Dimitrov, Thanks I'll try it out. @Martijn Laarman, can give you me link on how to update this as well? 90 seconds might be for this particular application but it would be nice to be able to set it as needed.
|
4

WebClient does not naturally support custom timeouts. But you can easily build a derived class with custom timeouts:

public class TimeoutWebClient : WebClient
{
    private int _timeOut = 10000;
    public int TimeOut
    {
        get
        {
            return _timeOut;
        }
        set
        {
            _timeOut = value;
        }
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest webRequest = base.GetWebRequest(address);
        webRequest.Timeout = _timeOut;
        return webRequest;
    }
}

Source: http://aspadvice.com/blogs/maniknet/archive/2008/06/16/Ganz-kurz_3A00_-WebClient-mit-eigenem-Verbindungs-Timeout-_2800_WebClient-with-a-custom-connection-timeout_2900_.aspx

Comments

2

Maybe you should go with

System.Net.WebRequest.Timeout
property

Comments

2

You could check for WebTimeout exception from server side then use SignalR to actively send timeout messsage back to clients:

var request = (HttpWebRequest)WebRequest.Create("http://www.your_third_parties_page.com");
request.Timeout = 1000; //Timeout after 1000 ms

try
{
    using (var stream = request.GetResponse().GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
         Console.WriteLine(reader.ReadToEnd());
    }
}
catch (WebException ex)
{
    if (ex.Status == WebExceptionStatus.Timeout)
    {
         //If timeout then send SignalR message to inform the clients...
         var context = GlobalHost.ConnectionManager.GetHubContext<YourHub>();
         context.Clients.All.addNewMessageToPage("This behavious have been processing too long!");
    }
}

See more how to setup SignalR for asp.net here

Comments

0

Many classes within .Net framework that involve any kind of networking include a Timeout property. For instance, there's such a property on the WebRequest class (System.Net)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.