467,885 Members | 1,311 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,885 developers. It's quick & easy.

Proxy application with HttpWebRequest

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;
}
Nov 16 '05 #1
  • viewed: 5597
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Abhishek Srivastava | last post: by
1 post views Thread by Imran Aziz | last post: by
3 posts views Thread by Codex Twin | last post: by
2 posts views Thread by =?Utf-8?B?TGFycnlLdXBlcm1hbg==?= | last post: by
2 posts views Thread by =?Utf-8?B?TGVuc3Rlcg==?= | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.