473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
0 6040

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Abhishek Srivastava | last post by:
Hello All, How do I completly and totally disable any kind of caching when making a HttpWebRequest. I have an application which downloads a file from the web. This file is updated on a daily...
1
by: Imran Aziz | last post by:
Hello All, I am using HttpWebRequest to fetch webpages in my ASP.net C# application. The request works fine without the proxy, but on using the code from within a network that uses proxy the...
4
by: Morten Snedker | last post by:
I'm trying to download a file. I've tried both using webclient and the httpWebRequest. Either way it returns a "407 proxy authentication required". The program is to be run externally, so...
3
by: Codex Twin | last post by:
Hello apologies if this is the wrong newsgroup to be sending this to. Basically, I have an ASP.NET application that I am trying to force to use a proxy server settings. This can be done by...
0
by: deepak | last post by:
Hi All, i m develpoing a .net Window application which has 4 texboxes(tbxURLmfor URL in URL Frame,tbxAddress for address in Proxy server frame,tbxPort for port in proxy server...
0
by: Maxx57 | last post by:
C# VS.Net 1.1 (2003) - trouble with using IE proxy server settings I've got a program that detects if it can connect to a webservice. It sends a System.Net.WebRequest and then checks the...
4
by: retroviz | last post by:
Hi there. I have written a screen scraping application (both web based and windows forms) in vb.net. When testing on a public broadband link it works fine. However it fails at work due to our...
2
by: =?Utf-8?B?TGFycnlLdXBlcm1hbg==?= | last post by:
Our WebDev team seems to have found a problem that exposes a bug in .NET 2.0. This problem can be shown when trying to access a WebService using SSL and through a proxy server after using the...
2
by: =?Utf-8?B?TGVuc3Rlcg==?= | last post by:
A C# (.NET 2) application which uses the System.Net.HttpWebRequest object to request a resource over HTTPS is failing following the installation of a new proxy server on our internal network with...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.