简体   繁体   中英

Inheriting from the web client class

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,

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; 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM