A simple Http request forwarder and request forwarding
I have been looking at development again these two days. I have built an IIS server on my computer for development and can access the internet. The key is that debugging is too troublesome !!
After I write the code, I need to publish the code to IIS to receive the message. However, in this process, I do not know what the code is, and it is difficult to debug bugs,
I have to find out where there is a Bug. modify the code and release it again!
Is there a way for me to set a breakpoint as usual?
So I wrote a simple Http request forwarder.
The principle is as follows: first request my IIS server. the IIS server forwards the request to the IIS Express and IIS Express of Vs through the following module.
Then return the response content to IIs. IIs finally sends the message
Check the Code:
Public class Transformer: IHttpModule {public void Dispose () {} public void Init (HttpApplication context) {context. beginRequest + = context_BeginRequest ;} /// <summary> /// target Url to which the Http request is forwarded /// </summary> public Uri ToUrl {get {// read string toUrl = FROM THE CONFIGURATION system. configuration. configurationManager. deleetask[ "TransToURL"]; // determines whether the Url is/if (! ToUrl. endsWith ("/") {toUrl = toUrl + "/";} Uri uri = new Uri (toUrl); return uri ;}} /// <summary> /// target UrlHost /// </summary> public string ToUrlHost {get {return ToUrl. host ;}//< summary> /// port of the target Url /// </summary> public string ToPort {get {var result = Regex. match (ToUrl. toString (), @ "^ http://.+ :( \ D +) ", RegexOptions. ignoreCase); if (result. groups. count> 1) {return result. groups [1]. value;} else {return "" ;}}/// <summary> // The Url directly requested by the client, that is, the Url of the application, all requests to this Url will be forwarded to the target Url // </summary> public Uri FromUrl {get; set ;} /// <summary> /// this application Url Host /// </summary> public string FromUrlHost {get {return FromUrl. host ;}//< summary> /// Url port of the application // </summary> public string FromPort {get {var result = Regex. match (FromUrl. toString (), @ "^ http://.+ :( \ D +) ", RegexOptions. ignoreCase); if (result. groups. count> 1) {return result. groups [1]. value;} else {return "" ;}} void context_BeginRequest (object sender, EventArgs e) {string toUrl = this. toUrl. toString (); HttpApplication app = sender as HttpApplication; var respone = app. response; var request = app. request; // initialize the application Url FromUrl = new Uri (request. url. toString (); // obtain the Url after the conversion target // Replace the Url with the target Url string tempUrl = this. replaceHostAndPort (FromUrl. toString (), TransType. transTo); // create an Http request to send the replaced request message to the target Url HttpWebRequest hRequest = HttpWebRequest. createHttp (tempUrl); // sets the request header this. setRequestHead (hRequest, request); # region sets the special request header if (! String. IsNullOrEmpty (request. Headers ["Accept"]) {hRequest. Accept = request. Headers ["Accept"];} if (! String. isNullOrEmpty (request. headers ["Connection"]) {string connection = request. headers ["Connection"]; hRequest. keepAlive = string. compare (connection, "keep-alive", StringComparison. currentCultureIgnoreCase) = 0;} if (! String. IsNullOrEmpty (request. Headers ["Content-Type"]) {hRequest. ContentType = request. Headers ["Content-Type"];} if (! String. IsNullOrEmpty (request. Headers ["wrong CT"]) {hRequest. Wrong Ct = request. Headers ["wrong CT"];} if (! String. IsNullOrEmpty (request. Headers ["Date"]) {hRequest. Date = Convert. ToDateTime (request. Headers ["Date"]);} if (! String. IsNullOrEmpty (request. Headers ["Host"]) {hRequest. Host = this. ToUrlHost;} if (! String. isNullOrEmpty (request. headers ["If-Modified-Since"]) {hRequest. ifModifiedSince = Convert. toDateTime (request. headers ["If-Modified-Since"]);} if (! String. isNullOrEmpty (request. headers ["Referer"]) {hRequest. referer = this. replaceHostAndPort (request. headers ["Referer"], TransType. transTo);} if (! String. IsNullOrEmpty (request. Headers ["User-Agent"]) {hRequest. UserAgent = request. Headers ["User-Agent"];} if (! String. isNullOrEmpty (request. headers ["Content-Length"]) {hRequest. contentLength = Convert. toInt32 (request. headers ["Content-Length"]);} # endregion // determines whether the request is a Get request. if not, write the request to the style if (String. compare (request. httpMethod, "get", StringComparison. currentCultureIgnoreCase )! = 0) {// set the Request body this. setRequestBody (hRequest, request);} // obtain the Response Message WebResponse hRespone = null; try {hRespone = hRequest. getResponse ();} catch (Exception exp) {respone. write (exp. message); respone. end () ;}// set the Response Header this. setResponeHead (hRespone, respone); # region sets the Special Response Header if (! String. IsNullOrEmpty (hRespone. Headers ["Content-Type"]) {respone. ContentType = hRespone. Headers ["Content-Type"];} if (! String. IsNullOrEmpty (hRespone. Headers ["Host"]) {respone. AddHeader ("Host", FromUrlHost);} if (! String. isNullOrEmpty (hRespone. headers ["Referer"]) {respone. addHeader ("Referer", this. replaceHostAndPort (hRespone. headers ["Referer"], TransType. transBack);} # endregion // write the response content this. setResponeBody (hRespone, respone); respone. end ();} /// <summary> /// set the request header /// </summary> /// <param name = "nrq"> </param> /// <param name = "orq"> </param> private void SetRequestHead (WebRequest nrq, httpRequest orq) {fo Reach (var key in orq. headers. allKeys) {try {nrq. headers. add (key, orq. headers [key]);} catch (Exception) {continue ;}}} /// <summary> /// set the style of the request report /// </summary> /// <param name = "nrq"> </param> /// <param name = "orq"> </param> private void SetRequestBody (WebRequest nrq, httpRequest orq) {nrq. method = "POST"; var nStream = nrq. getRequestStream (); byte [] buffer = new byte [1024*2]; int rLength = 0; do {rLength = orq. inputStream. read (buffer, 0, buffer. length); nStream. write (buffer, 0, rLength);} while (rLength> 0 );} /// <summary> /// set the Response Header /// </summary> /// <param name = "nrp"> </param> /// <param name = "orp"> </param> private void SetResponeHead (WebResponse nrp, httpResponse orp) {foreach (var key in nrp. headers. allKeys) {try {orp. headers. add (key, nrp. headers [key]);} catch (Exception) {Continue ;}}} /// <summary> /// set the response style // </summary> /// <param name = "nrp"> </param> // <param name = "orp"> </param> private void SetResponeBody (WebResponse nrp, httpResponse orp) {var nStream = nrp. getResponseStream (); byte [] buffer = new byte [1024*2]; int rLength = 0; do {rLength = nStream. read (buffer, 0, buffer. length); orp. outputStream. write (buffer, 0, rLength);} while (rLength> 0) ;} /// <Summary> /// replace Host and Port /// </summary> /// <param name = "url"> </param> /// <param name = "type"> </param> // <returns> </returns> private string ReplaceHostAndPort (string url, transType type) {string tempToPortStr = string. isNullOrEmpty (ToPort )? "": ":" + ToPort; string tempFromPortStr = string. IsNullOrEmpty (FromPort )? "": ":" + FromPort; if (type = TransType. transBack) {return url. replace (ToUrlHost + tempToPortStr, FromUrlHost + tempFromPortStr);} else {return url. replace (FromUrlHost + tempFromPortStr, ToUrlHost + tempToPortStr) ;}} public enum TransType {TransTo, TransBack}