Hi folks,
I have an aspx application App A hosted in a server which is inside
the intranet [or in some way blocked for free access from the
internet]. I need to serve pages from this application _through_
another application App B which acts as a proxy [App B can ofcourse
access App A].
App B just has a HttpHandler which recieves the request for pages
(actualy residing in App A), creates a HttpWebRequest object (which
inturn reads the context.HttpRequest object) and serves the page with
HttpWebResponse object (writing the result into content.Response
object).
The problem is I'm not able to take the cookies / Forms collection
values across the client broswer-App B-App A (See commented code) and
any request with POSTed data is timing out.
Can anyone please tell me how can I transfer Cookie and Form
collection data using HttpWebRequest and HttpWebResponse objects?
public void ProcessRequest(HttpContext context)
{
int loop1;
//read values from configuration file
string remoteWebSite =
ConfigurationSettings.AppSettings["RemoteWebSite"];
string remoteUrl;
remoteUrl = "/AppA";
context.Request.Url.AbsoluteUri.Replace(context.Re quest.ApplicationPath,
remoteWebSite);
//create the web request to get the remote stream
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(remoteUrl);
request.ContentLength = context.Request.TotalBytes;
request.Method = "POST";
request.ContentType="application/x-www-form-urlencoded";
request.Headers.Add(context.Request.Form);
request.KeepAlive = false;
//request.AllowWriteStreamBuffering = false;
//////////////////////////////////////////////////////////////////////////////////////////
// // Copy the cookies from context.Request to the
request.CookieContainer
// CookieContainer newCookies = new CookieContainer();
// for (loop1=0; loop1 < context.Request.Cookies.Count;
loop1++)
// {
// System.Web.HttpCookie hc =
context.Request.Cookies[loop1];
// Cookie c = new Cookie(hc.Name, hc.Value);
// if (context.Request.Cookies[loop1].Domain == null)
// c.Domain =
context.Request.ServerVariables["HTTP_HOST"].ToString();
// else
// c.Domain =
context.Request.Cookies[loop1].Domain;
//
// newCookies.Add(c);
// }
//
// request.CookieContainer = newCookies;
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
// // Copy context.Request Form elements to request
// NameValueCollection coll;
// coll = context.Request.Form;
// String[] arr1 = coll.AllKeys;
// string postData = "";
//
// for (loop1 = 0; loop1 < arr1.Length; loop1++)
// postData += (arr1[loop1] + "=" +
context.Request.Form[arr1[loop1]] + "&");
//
// ASCIIEncoding encoding = new ASCIIEncoding();
// byte[] data = encoding.GetBytes(postData);
//
// Stream newStream = request.GetRequestStream();
//
// for (loop1 = 0; loop1 < data.Length; loop1++)
// {
// newStream.WriteByte(data[loop1]);
// }
// newStream.Close();
//////////////////////////////////////////////////////////////////////////////////////////
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch(System.Net.WebException we)
{
//remote url not found, send 404 to client
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Not Found";
context.Response.Write("<h2>Page not found</h2>");
context.Response.Write("<h2>" + we.ToString() +
"</h2>");
context.Response.End();
return;
}
Stream receiveStream = response.GetResponseStream();
if ((response.ContentType.ToLower().IndexOf("html")>= 0)||(response.ContentType.ToLower().IndexOf("javas cript")>=0))
{
//this response is HTML Content, so we must parse it
StreamReader readStream = new StreamReader
(receiveStream, Encoding.Default);
Uri test = new Uri(remoteUrl);
string content;
content=
ParseHtmlResponse(readStream.ReadToEnd(),context.R equest.ApplicationPath);
//write the updated HTML to the client
context.Response.Write(content);
//close streams
readStream.Close();
}
else
{
//the response is not HTML Content
byte[] buff = new byte[1024];
int bytes = 0;
while( ( bytes = receiveStream.Read( buff, 0, 1024 )
) > 0 )
{
//Write the stream directly to the client
context.Response.OutputStream.Write (buff, 0,
bytes );
}
//close streams
}
response.Close();
receiveStream.Close();
context.Response.End();
}
public string ParseHtmlResponse(string html,string appPath)
{
html=html.Replace("\"/","\""+appPath+"/");
html=html.Replace("'/","'"+appPath+"/");
html=html.Replace("=/","="+appPath+"/");
return html;
}
public string ParseURL(string url)
{
if (url.IndexOf("http/")>=0)
{
string
externalUrl=url.Substring(url.IndexOf("http/"));
return externalUrl.Replace("http/","http://") ;
}
else
return url;
}