<%@ WebHandler Language="C#" Debug="True" Class="Microshaoft.HttpProxyHandler" %>
namespace Microshaoft
{
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
public class HttpProxyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpWebRequest httpWebRequest = null;
WebResponse webResponse = null;
try
{
var request = context.Request;
string url = request["fw"];
byte[] buffer = ReadStreamToBytes(request.InputStream);
httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
httpWebRequest.Method = request.HttpMethod;
httpWebRequest.ContentType = request.ContentType;
httpWebRequest.UserAgent = request.UserAgent;
httpWebRequest.KeepAlive = false;
ServicePointManager.Expect100Continue = false;
//httpWebRequest.SendChunked = true;
//httpWebRequest.TransferEncoding = request.ContentEncoding.EncodingName;
if
(
request.Headers.AllKeys.Any
(
(x) =>
{
return (x.ToLower() == "soapaction");
}
)
)
{
httpWebRequest.Headers.Add("SOAPAction", request.Headers["SOAPAction"]);
}
httpWebRequest.Headers.Add("X-Forwarded-For", request.UserHostAddress);
httpWebRequest.Timeout = 10 * 1000;
if (buffer.Length > 0)
{
using (Stream stream = httpWebRequest.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
}
webResponse = httpWebRequest.GetResponse();
using (Stream stream = webResponse.GetResponseStream())
{
buffer = ReadStreamToBytes(stream);
stream.Close();
}
var response = context.Response;
response.BinaryWrite(buffer);
}
catch (Exception e)
{
throw new Exception("OrignalInnerException", e);
}
finally
{
if (webResponse != null)
{
webResponse.Close();
webResponse = null;
}
if (httpWebRequest != null)
{
httpWebRequest.Abort();
httpWebRequest = null;
}
}
}
private byte[] ReadStreamToBytes(Stream stream)
{
byte[] buffer = new byte[64 * 1024];
MemoryStream ms = new MemoryStream();
int r = 0;
int l = 0;
long position = -1;
if (stream.CanSeek)
{
position = stream.Position;
stream.Position = 0;
}
while (true)
{
r = stream.Read(buffer, 0, buffer.Length);
if (r > 0)
{
l += r;
ms.Write(buffer, 0, r);
}
else
{
break;
}
}
byte[] bytes = new byte[l];
ms.Position = 0;
ms.Read(bytes, 0, (int)l);
ms.Close();
ms = null;
if (position >= 0)
{
stream.Position = position;
}
return bytes;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
/*
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<META NAME="Generator" CONTENT="EditPlus,Microshaoft">
<META NAME="Author" CONTENT="EditPlus,Microshaoft">
<META NAME="Keywords" CONTENT="EditPlus,Microshaoft">
<META NAME="Description" CONTENT="EditPlus,Microshaoft">
</head>
<body>
<form method="post" action="http://local.asp.net/proxy.ashx" target="_blank">
<input type="text" name="a" />
<input type="text" name="fw" value="http://interface.morsing.local/test.aspx" />
<input type="submit" />
</form>
</body>
</html>
*/
|