views:

782

answers:

4

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
{
}
A: 

Maybe you should go with

System.Net.WebRequest.Timeout
property

bottlenecked
A: 

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)

Damien_The_Unbeliever
+1  A: 

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

Jens
+1  A: 

You could use the Timeout property:

var request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Timeout = 1000;
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;
using (var stream = request.GetResponse().GetResponseStream())
using (var reader = new StreamReader(stream))
{
    var xel = XElement.Load(reader);
}
Darin Dimitrov
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?
Victor
Of course, putting try/catch is what you need to handle the exception and return a proper message.
Darin Dimitrov
This may be for another question, but do you know how I can also use this with Xelement.Load(uri)?
Victor
@victor_foster, please see my update.
Darin Dimitrov
@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.
Martijn Laarman
@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.
Victor