| « How to improve your intranet ASP.NET web application performance ? | Bonjour Updated » |
How to make an HTTP proxy
As you have seen, I have recently updated my zeroconf codeplex project. This project not only contains an implementation of the DNS protocol and of the Bonjour protocol. It also contains an implmentation of the HTTP protocol to be able to send some HTTP request on UDP, which is not possible with the implementation given with the .NET Framework. At work, I have to check that a web application I developped works correctly with IE6 (yes, it still exists. unfortunately). Since I am on Windows 7, I thought it could be great to use IE6 as virtual app. I think you know where I want to come to. On a virtual machine, you have no access to the web development server started by visual studio. What I could have been doing is deploying to the virtual machine, which would mean installing IIS, configuring it, spend time to check the web.config. Well not a straight forward solution. So I told myself : What I need is a proxy, and I already have the HTTP protocol implementation !
Here is the code of the famous proxy :
public class Proxy
{
RestServer proxy;
string hostToRedirectTo;
static void Main(string[] args)
{
Proxy p = new Proxy(ushort.Parse(args[0]), args[1]);
p.Start();
Console.ReadLine();
p.Stop();
}
private void Stop()
{
proxy.Stop();
}
private void Start()
{
proxy.StartTcp();
}
public Proxy(ushort portToListenOn, string hostToRedirectTo)
{
proxy = new RestServer(portToListenOn);
this.hostToRedirectTo = hostToRedirectTo;
proxy.HttpRequestReceived += proxy_HttpRequestReceived;
}
void proxy_HttpRequestReceived(object sender, HttpRequestEventArgs e)
{
e.Request.Host = this.hostToRedirectTo;
e.Response = e.Request.GetResponse(true);
e.Response.Body.Position = 0;
}
}
As you can see, the solution is straight forward and is only a ten lines of code !!!
1 comment
piece of writing provides pleasant understanding even.
Comments are closed for this post.