As an example I am trying to alter the GetResponse() function of 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.
}
}
However I can’t find the standard code for GetResponse() anywhere. Am I expected to write one from scratch? I’d like to Copy+Paste the original code in and then alter it. I already tried looking here but that didn’t help much.
To test if I was even on the right track, I commented out GetResponse() and tried to use the HttpWebRequest_Override as-is (with only the constructor in).
And then using it like:
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; }
}
But this throws the error:
Unable to cast object of type 'System.Net.HttpWebRequest' to type 'bulktest.HttpWebRequest_Override'.
So two questions:
Why isn’t the class working, and where can I find the code for .NET classes so I can alter them?
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:
Where your class might look like:
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.