Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting from the web client class

Tags:

c#

inheritance

C# 2008

I am not sure how much work there is to inheriting from the web client class.

Currently I am using it in my project. And I can't change to anything else. The customer would like to have a timeout after a certain period of time. The web client doesn't have this.

So rather than re-invent the wheel, I am thinking of inheriting from the web client and adding this property.

Do you think this is a suitable solution? Could it mean more work just to add this. What is the easiest way to go about this?

Many thanks,

like image 308
ant2009 Avatar asked Dec 01 '25 05:12

ant2009


1 Answers

extend webclient to add a timeout property that can optionally be set in an overloaded constructor (a timeout of 0 or less means no timeout). Then override the GetWebRequest method:

protected override WebRequest GetWebRequest(Uri address) 
{ 
    var req = base.GetWebRequest(address); 
    var httpReq = req as HttpWebRequest;
    if (httpReq != null && Timeout > 0) 
    {
        httpReq.Timeout = Timeout; 
    } 

    return req; 
} 
like image 194
Luke Schafer Avatar answered Dec 03 '25 17:12

Luke Schafer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!