2

作为一个例子,我试图GetResponse()改变HttpWebRequest

class HttpWebRequest_Override : HttpWebRequest
{
    public HttpWebRequest_Override(System.Runtime.Serialization.SerializationInfo SerializationInfo, System.Runtime.Serialization.StreamingContext StreamingContext) 
        : base(SerializationInfo, StreamingContext) { }

    public override WebResponse GetResponse()
    {
        //Edit GetResponse() code here.
    }
}

但是我找不到GetResponse()任何地方的标准代码。我应该从头开始写一个吗?我想复制+粘贴原始代码,然后对其进行更改。我已经尝试过查看这里,但这并没有太大帮助。

为了测试我是否走在正确的轨道上,我注释掉GetResponse()并尝试按HttpWebRequest_Override原样使用(只有构造函数)。

然后像这样使用它:

public static string DownloadText(string url)
{
    try
    {
        var request = (HttpWebRequest_Override)WebRequest.Create(url);
        request.Proxy = null;
        var response = (HttpWebResponse)request.GetResponse();

        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            return reader.ReadToEnd();
        }
    }
    catch (Exception ex) { return "error_program : "+ex.Message; }
}

但这会引发错误:

Unable to cast object of type 'System.Net.HttpWebRequest' to type 'bulktest.HttpWebRequest_Override'.

所以两个问题:

为什么类不工作,我在哪里可以找到 .NET 类的代码以便我可以更改它们?

4

3 回答 3

1

WebRequest.Create doesn't create an instance of your class, it creates an instance of HttpWebRequest, which you derive from. While you can cast an instance of a child as it's parent, you can't cast an instance of the parent as the child. The parent isn't guaranteed to have all of the methods of the child, whereas the child is guaranteed to have to the methods of the parent.

You can register custom protocols that are handled by your own, derived class or, via configuration, register your class to handle the default protocols. Whether you need to do this is an open question. From your example, a simple wrapper might be sufficient. The wrapper would take a HttpWebRequest instance in the constructor and delegate the standard WebRequest methods to that instance. Any new methods would use the underlying instance's methods for their work and layer your additional functionality on top of it.

EX:

 var request = new WebRequestWrapper((HttpWebRequest)WebRequest.Create(url));
 var html = request.DownloadText();

Where your class might look like:

 public class WebRequestWrapper
 {
      private HttpWebRequest _request;

      public WebRequestWrapper(HttpWebRequest request)
      {
          this._request = request;
      }

      public string DownloadText()
      {
          using (var response = (HttpWebResponse)this._request.GetResponse())
          using (var reader = new StreamReader(response.GetResponseStream()))
          {
              return reader.ReadToEnd();
          }
      }
 }

FWIW, I don't like catching the exception and returning a string. I'd rather let the exception bubble up since the method returns a string.

于 2012-04-29T13:29:53.753 回答
1

可以通过 .config 文件“注入”自定义 Web 请求类型。

看看这里:http: //msdn.microsoft.com/en-us/library/bc0fhzk5.aspx

于 2012-04-29T13:28:28.793 回答
0

You can browse .NET Framework source in different ways, for example, via .NET Reflector. But you can also see it online - for example, here's the source of HttpWebRequest.GetResponse():

http://typedescriptor.net/browse/members/326374-System.Net.HttpWebRequest.GetResponse()

As noted by others, you don't have to copy-paste the original code. But it's still good to know how it works.

于 2012-04-29T13:30:54.660 回答