HttpWebRequest 后代

发布于 2024-12-21 18:52:09 字数 1111 浏览 1 评论 0原文

我正在尝试使用

// get a login page
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.facebook.com/");   

我配置的类 FacebookHttpWebRequest 而不是通常的 HttpWebRequest ,它看起来像这样:

// get a login page
FacebookHttpWebRequest req = (FacebookHttpWebRequest)WebRequest.Create("http://www.facebook.com/");   

Here is generated class implementation

class FacebookHttpWebRequest : HttpWebRequest
    {
        public FacebookHttpWebRequest() : base()
        {
            this.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1";
            this.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";            
        }
    }

but there is a error raise Error

“System.Net.HttpWebRequest”不包含构造函数 接受 0 个参数

,而且它确实需要两个参数:serializationInfostreamingContext

那么我怎样才能创建自己的 HttpWebRequest 后代呢?我知道我可以使用特殊的方法来配置通常的 HttpWebRequest,但是为此拥有特殊的类会更优雅。

I'm trying to use instead of usual HttpWebRequest

// get a login page
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.facebook.com/");   

my configured class FacebookHttpWebRequest, that look like this:

// get a login page
FacebookHttpWebRequest req = (FacebookHttpWebRequest)WebRequest.Create("http://www.facebook.com/");   

Here is configured class implementation

class FacebookHttpWebRequest : HttpWebRequest
    {
        public FacebookHttpWebRequest() : base()
        {
            this.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1";
            this.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";            
        }
    }

but there is a error raised Error

'System.Net.HttpWebRequest' does not contain a constructor that
takes 0 arguments

And it really requires two arguments: serializationInfo and streamingContext.

So how can I make my own HttpWebRequest descendant? I know I can use special method that will configure usual HttpWebRequest, but it will be more elegantly to have special class for that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2024-12-28 18:52:09

为此,您不需要继承。只需将创建登录名包装在您自己的工厂中即可:

static class FacebookWebRequestFactory {
  public static HttpWebRequest Create() {
    var req = (HttpWebRequest)WebRequest.Create("http://www.facebook.com/");
    req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1";
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";            
    return req;
  }
}

像这样使用它:

var req = FacebookWebRequestFactory.Create();

You don't need inheritance for this. Just wrap the creation login in your own factory:

static class FacebookWebRequestFactory {
  public static HttpWebRequest Create() {
    var req = (HttpWebRequest)WebRequest.Create("http://www.facebook.com/");
    req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1";
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";            
    return req;
  }
}

Use it like this:

var req = FacebookWebRequestFactory.Create();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文