Relaying an HttpRequest in ASP.NET

Following up on my previous post, Relaying an HttpWebResponse in ASP.NET MVC, I’ve also made an extension method that does the heavy lifting when forwarding/proxying/tunnelling/mirroring an HTTP request.

It copies the unrestricted headers verbatim (e.g. “Accept-Encoding”, “Cookie”), manually sets the restricted headers (e.g. “Content-Type”) and leaves the IIS level headers blank (e.g. “Connection”, “Content-Length”).  It also checks to see whether a content-body is included (e.g. a POST request) and copies the stream if so. 

Usage (in an MVC action or a WebForms codebehind method):

var postRequest = (HttpWebRequest)WebRequest.Create("");
var response = (HttpWebResponse)postRequest.GetResponse();

How easy is that!