-
-
Save mahizsas/3a3a297a253f0b0a3f41 to your computer and use it in GitHub Desktop.
Sample proxy for http request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ WebHandler Language="C#" Class="PassThruProxy" Debug="true" %> | |
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Text; | |
using System.Web; | |
public class PassThruProxy : IHttpHandler { | |
public PassThruProxy( ) { } | |
public void ProcessRequest( HttpContext Context ) { | |
HttpRequest Request = Context.Request; | |
HttpResponse Response = Context.Response; | |
string URI = null; | |
try { | |
URI = Request.Url.Query.Substring( 1 ); | |
} catch ( Exception Passless ) { | |
Response.StatusCode = 500; | |
Response.StatusDescription = "Parameter Missing"; | |
Response.Write( "The parameter that makes this proxy worthwhile and functioning was not given." ); | |
Response.End( ); | |
return; | |
} | |
System.Net.HttpWebRequest ProxyRequest = ( System.Net.HttpWebRequest )System.Net.HttpWebRequest.Create( URI ); | |
ProxyRequest.Method = Request.HttpMethod; | |
ProxyRequest.ServicePoint.Expect100Continue = false; | |
ProxyRequest.Referer = Request.Headers[ "referer" ]; | |
if ( Request.InputStream.Length > 0 ) { | |
byte[ ] Bytes = new byte[ Request.InputStream.Length ]; | |
Request.InputStream.Read( Bytes, 0, ( int )Request.InputStream.Length ); | |
ProxyRequest.ContentLength = Bytes.Length; | |
string ContentType = Request.ContentType; | |
if ( String.IsNullOrEmpty( ContentType ) ) { | |
ProxyRequest.ContentType = "application/x-www-form-urlencoded"; | |
} else { | |
ProxyRequest.ContentType = ContentType; | |
} | |
using ( Stream OutputStream = ProxyRequest.GetRequestStream( ) ) { | |
OutputStream.Write( Bytes, 0, Bytes.Length ); | |
} | |
} else { | |
ProxyRequest.Method = "GET"; | |
} | |
System.Net.WebResponse ServerResponse = null; | |
try { | |
ServerResponse = ProxyRequest.GetResponse( ); | |
} catch ( System.Net.WebException WebEx ) { | |
Response.StatusCode = 500; | |
Response.StatusDescription = WebEx.Status.ToString( ); | |
Response.Write( WebEx.Response ); | |
Response.End( ); | |
return; | |
} | |
if ( ServerResponse != null ) { | |
Response.ContentType = ServerResponse.ContentType; | |
using ( Stream ByteStream = ServerResponse.GetResponseStream( ) ) { | |
if ( ServerResponse.ContentType.Contains( "text" ) || | |
ServerResponse.ContentType.Contains( "json" ) || | |
ServerResponse.ContentType.Contains( "xml" ) ) { | |
using (StreamReader Reader = new StreamReader( ByteStream ) ) { | |
string ResponseString = Reader.ReadToEnd( ); | |
Response.CacheControl = "no-cache"; | |
Response.Write( ResponseString ); | |
} | |
} else { | |
BinaryReader BinReader = new BinaryReader( ByteStream ); | |
byte[] BinaryOutputs = BinReader.ReadBytes( ( int )ServerResponse.ContentLength ); | |
BinReader.Close( ); | |
Response.CacheControl = "no-cache"; | |
Response.OutputStream.Write( BinaryOutputs, 0, BinaryOutputs.Length ); | |
} | |
ServerResponse.Close( ); | |
} | |
} | |
Response.End( ); | |
} | |
public bool IsReusable { | |
get { return false; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment