It occurs that we want some content from URL but that is in the provided time. To achieve things, we use web request or Webclient object. Web request provides timeout property but you will not find anything for the Webclient.
In Webclient you need to write custom class that is derived from the Webclient and add timeout property. Timeout is very important when you required to know result in the time duration.
Below is the new class derived from the WebClient and added timeout property to it.
public class CGWebClient : System.Net.WebClient
{
private int timeout;
public int Timeout
{
get { return timeout; }
set { timeout = value; }
}
public CGWebClient()
{
timeout = -1;
}
protected override System.Net.WebRequest GetWebRequest(Uri address)
{
System.Net.WebRequest request = base.GetWebRequest(address);
if (request.GetType() == typeof(System.Net.HttpWebRequest))
{
((System.Net.HttpWebRequest)request).Timeout = timeout;
}
return request;
}
}
Now when ever you require just initiatie the object of CGWebClient instead of WebClient.
No comments:
Post a Comment