Question : Create a subclassof HttpWebRequest

In order to use HttpWebRequest you have to use cast a WebRequest as HttpWebRequest (hope I am saying that correctly).  I have a class that I made that creates the HttpWebRequest and my class has methods for creating a POST string and authenticating to grab a cookie and then another POST with a file to upload.

What I want to do is turn this into a more generic class as a subclass of HttpWebRequest, but I don't know how to create a subclass of HttpWebRequest since casting from WebRequest is required.

Anyone know how this would be done?
1:
2:
WebRequest request = WebRequest.Create(url);
HttpWebRequest httpreq = (HttpWebRequest)request;

Answer : Create a subclassof HttpWebRequest

You wouldn't derive from it, but could wrap it.  Here's a silly example that shows you can make your own class to handle things in your way - in this example the constructor takes a domain name and automatically prepends "http://www." and appends ".com/" (demonstrating a behavior that extends HttpWebRequest)...

The actual HttpWebRequest is exposed as a property, and you may choose to wrap commonly used functions and properties such as GetResponse()...

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net;

static class Program
{
	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	[STAThread]
	static void Main()
	{
		MyWebRequest myReq = new MyWebRequest("experts-exchange");

		MessageBox.Show(myReq.GetResponse().ContentType);
	}
}
public class MyWebRequest
{
	private HttpWebRequest _webRequest;

	public MyWebRequest(string Address)
	{
		_webRequest = (HttpWebRequest)WebRequest.Create("http://www." + Address + ".com/");
	}

	public HttpWebRequest Request
	{
		get { return _webRequest; }
	}

	public WebResponse GetResponse()
	{
		return _webRequest.GetResponse();
	}
}
Random Solutions  
 
programming4us programming4us