简体   繁体   English

继承自Web客户端类

[英]Inheriting from the web client class

C# 2008 C#2008

I am not sure how much work there is to inheriting from the web client class. 我不确定继承Web客户端类有多少工作要做。

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. Web客户端没有此功能。

So rather than re-invent the wheel, I am thinking of inheriting from the web client and adding this property. 因此,我不是重新发明轮子,而是考虑继承Web客户端并添加此属性。

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). 扩展webclient以添加一个超时属性,该属性可以选择在重载的构造函数中设置(超时为0或更小意味着没有超时)。 Then override the GetWebRequest method: 然后重写GetWebRequest方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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