HttpWebRequest 后代
我正在尝试使用
// 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 个参数
,而且它确实需要两个参数:serializationInfo
和 streamingContext
。
那么我怎样才能创建自己的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您不需要继承。只需将创建登录名包装在您自己的工厂中即可:
像这样使用它:
You don't need inheritance for this. Just wrap the creation login in your own factory:
Use it like this: