D
Dave Mathew
I'm attempting to write a reverse proxy in ASP.Net which will catch
requests to my server in ASP.Net, perform some actions, and then get
the desired resource off another server to return to the original
client (using HttpWebRequest and rewriting it's response to my ASP.Net
client). I'm able to get the reverse proxy to work for html pages but
it's not working for images. Right now the code I'm playing with is
similar to this:
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
// Get image in another request
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create("http://
otherserver.com/image.gif");
// Copy header variables
foreach (String each in app.Request.Headers)
{
if (!WebHeaderCollection.IsRestricted(each))
webreq.Headers.Add(each,
app.Request.Headers.Get(each));
}
// Get Response (the image) and write it's stream back to the
original client
HttpWebResponse response = (HttpWebResponse)webreq.GetResponse();
Stream responseStream = response.GetResponseStream();
Stream output = app.Response.OutputStream;
copyStream(responseStream, output);
}
When I debug the code seems to be getting the stream for the image ok
(if I do this in a windows forms app I can get an image from the
stream and show it in a picture box just fine), but the browser
apparently isn't receiving the response it expects because it either
gives an error or if the requested image is part of another page it
shows the familiar image not found icon. I've looked at a few other
reverse proxy's in ASP.Net and they don't seem to be doing anything
special for different types of files so I'm not sure what I'm doing
wrong here... any suggestion? I thought maybe having this code in the
BeginRequest event may be part of the problem, but it works for HTML
pages so that makes me think it's something else I'm overlooking. Any
help would be greatly appreciated.
requests to my server in ASP.Net, perform some actions, and then get
the desired resource off another server to return to the original
client (using HttpWebRequest and rewriting it's response to my ASP.Net
client). I'm able to get the reverse proxy to work for html pages but
it's not working for images. Right now the code I'm playing with is
similar to this:
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
// Get image in another request
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create("http://
otherserver.com/image.gif");
// Copy header variables
foreach (String each in app.Request.Headers)
{
if (!WebHeaderCollection.IsRestricted(each))
webreq.Headers.Add(each,
app.Request.Headers.Get(each));
}
// Get Response (the image) and write it's stream back to the
original client
HttpWebResponse response = (HttpWebResponse)webreq.GetResponse();
Stream responseStream = response.GetResponseStream();
Stream output = app.Response.OutputStream;
copyStream(responseStream, output);
}
When I debug the code seems to be getting the stream for the image ok
(if I do this in a windows forms app I can get an image from the
stream and show it in a picture box just fine), but the browser
apparently isn't receiving the response it expects because it either
gives an error or if the requested image is part of another page it
shows the familiar image not found icon. I've looked at a few other
reverse proxy's in ASP.Net and they don't seem to be doing anything
special for different types of files so I'm not sure what I'm doing
wrong here... any suggestion? I thought maybe having this code in the
BeginRequest event may be part of the problem, but it works for HTML
pages so that makes me think it's something else I'm overlooking. Any
help would be greatly appreciated.