473,396 Members | 1,797 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,396 software developers and data experts.

Forwarding HTTP POST: how to return correct HTTP status?

I'm building a small ASPX page in C# to forward HTTP POST requests to
another site. When the other site returns an HTTP error such as 404 or
405, I want to return that same HTTP status value to my caller. But when
I do so, my caller always reports HTTP error 500. How can I return the
actual HTTP status that I received?

Here is my code:

private void Page_Load(object sender, System.EventArgs e)
{
bool failed = false; // No exceptions caught yet.
Exception copyOfException = null;
try {
string UrlForThisQuery = Global.NextServerUrl() + "BAD";

System.Net.WebClient client = new System.Net.WebClient();
byte [] byXmlOut = null;

if ( 0 == Request.Form.Count ) {

// First case: no parameters; this must be an HTTP GET.
// Perform an HTTP GET.

byXmlOut = client.DownloadData(UrlForThisQuery);
} else {

// Second (main) case: one or more parameters;
// this is a normal XML query in the form of HTTP POST.
// Just forward the POST.

byXmlOut = client.UploadValues(UrlForThisQuery,
Request.Form);
}

// Return the content of the page to the caller.
Response.BinaryWrite(byXmlOut);
}
catch (WebException ex) {
HttpWebResponse r = (HttpWebResponse)ex.Response;
copyOfException = new WebException(
ex.Message,
ex,
(WebExceptionStatus) r.StatusCode, // 404
ex.Response);
failed = true;
}

// Relay any exception to the caller.
if (failed) {
throw copyOfException; // But the caller reports 500.
}

// Normal exit. We already performed Response.BinaryWrite() above.
}

-- Phil Davidson, Oakland, CA
Nov 23 '05 #1
1 6420
Here's the answer: When I throw an exception from my ASPX page, it's
always reported as HTTP error 500. Instead I need to put the HTTP status
information into the Response object and return normally from the
Page_Load function:

catch (WebException ex) {
HttpWebResponse r = (HttpWebResponse)ex.Response;
copyOfException = new WebException(
ex.Message,
ex,
(WebExceptionStatus) r.StatusCode,
ex.Response);
Response.StatusCode = (int) r.StatusCode;
Response.StatusDescription =
"HTTP status returned from " + UrlForThisQuery + "\r\n"
+ ex.Message;

// CopyStream is a simple function that copies from the first
// stream to the second stream in blocks.
CopyStream(ex.Response.GetResponseStream(),
Response.OutputStream);
failed = true;
re_throw = false;
}
...
// Relay non-HTTP exceptions to the caller.
if (re_throw) {
throw copyOfException;
}

-- Phil Davidson

In microsoft.public.dotnet.framework.webservices,
go****@phildavidson.com wrote:
I'm building a small ASPX page in C# to forward HTTP POST requests to
another site. When the other site returns an HTTP error such as 404 or
405, I want to return that same HTTP status value to my caller. But when
I do so, my caller always reports HTTP error 500. How can I return the
actual HTTP status that I received?

Here is my code:

private void Page_Load(object sender, System.EventArgs e)
{
bool failed = false; // No exceptions caught yet.
Exception copyOfException = null;
try {
string UrlForThisQuery = Global.NextServerUrl() + "BAD";

System.Net.WebClient client = new System.Net.WebClient();
byte [] byXmlOut = null;

if ( 0 == Request.Form.Count ) {

// First case: no parameters; this must be an HTTP GET.
// Perform an HTTP GET.

byXmlOut = client.DownloadData(UrlForThisQuery);
} else {

// Second (main) case: one or more parameters;
// this is a normal XML query in the form of HTTP POST.
// Just forward the POST.

byXmlOut = client.UploadValues(UrlForThisQuery,
Request.Form);
}

// Return the content of the page to the caller.
Response.BinaryWrite(byXmlOut);
}
catch (WebException ex) {
HttpWebResponse r = (HttpWebResponse)ex.Response;
copyOfException = new WebException(
ex.Message,
ex,
(WebExceptionStatus) r.StatusCode, // 404
ex.Response);
failed = true;
}

// Relay any exception to the caller.
if (failed) {
throw copyOfException; // But the caller reports 500.
}

// Normal exit. We already performed Response.BinaryWrite() above.
}

-- Phil Davidson, Oakland, CA

Nov 23 '05 #2

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

Similar topics

1
by: Binod Nair | last post by:
Hi All, I have an ASP.NET appication running on http://xx.xxx.xxxx.xx/aspApplication and I have a domain http://www.mydomain.com registered at godaddy.com for this application.I have setup...
6
by: Nick Horrocks | last post by:
I have set up a custom error page for 404 errors. However the HTTP status code returned is 302 followed by 200, this causes search engines not to remove old pages from their index. How can I...
0
by: DT | last post by:
I'm writing a .NET 1.1 client that invokes a method of a Web Service running on another organization's IIS 5 server. I need help understanding a fine point about HTTP, specifically in relation to...
4
by: andrew | last post by:
Hi there everyone. Right I have scoured everywhere to find out how to do this and I haven't the foggiest... I am building a shopping cart that uses an external clearing house . My shopping...
0
by: Anbu | last post by:
I already posted this message on usenet groups. But no response. I'm trying with the Microsoft community now, hoping to get some resolution. I have an Intermediate Web service developed using...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
1
by: sergiotb | last post by:
Hello everyone!! I'm trying to send a image (jpg) from a C# program to a CGI perl. The problem is that the code of the client C# is not working properly...I guess. The steps to send the image are: 1)...
2
by: MDANH2002 | last post by:
Hi From VB.NET I want to simulate the POST request of the following HTML form <html> <title>HTTP Post Testing</title> <body> <form action=http://www.example.com/postdata ...
1
by: redgoals | last post by:
Hi All, I will be creating a white label website for car hire dealers. My main website will be called something like: carhire.com, the content on carhire.com will be pulled in from a database. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.