HttpWebRequest ResponseStatus

C

Chizl

dev: Visual Studio 2008, C#

I'm looking for the ResponseStatus code from a web page once I use:

HttpWebRequest whr = (HttpWebRequest)WebRequest.Create(strURL);
whr.Method = "GET";

This next line will fail is there is a status 404 on the above create, but
after spending about an hour into this I'm not finding the status code
response.
WebResponse wr = whr.GetResponse();


I found a page that looked like there was a HttpWebRequest.ResponseStatus,
but when I look for it in the code, I couldn't find it. While I have it
running in memory, I've gone through and found something under the
"Non-Public Members" that says whr.ResponseStatusCode="NotFound". However,
it's private so I can't figure out the property I need to call to access it.

I have a try catch, and the failure occurs on the whr.GetResponse saying the
web server responding with "404" as the Exception Message. I would prefer
to know the status before I throw an error.
 
A

Arne Vajhøj

Chizl said:
I'm looking for the ResponseStatus code from a web page once I use:

HttpWebRequest whr = (HttpWebRequest)WebRequest.Create(strURL);
whr.Method = "GET";

This next line will fail is there is a status 404 on the above create, but
after spending about an hour into this I'm not finding the status code
response.
WebResponse wr = whr.GetResponse();

I found a page that looked like there was a HttpWebRequest.ResponseStatus,
but when I look for it in the code, I couldn't find it. While I have it
running in memory, I've gone through and found something under the
"Non-Public Members" that says whr.ResponseStatusCode="NotFound". However,
it's private so I can't figure out the property I need to call to access it.

I have a try catch, and the failure occurs on the whr.GetResponse saying the
web server responding with "404" as the Exception Message. I would prefer
to know the status before I throw an error.

wr.StatusCode has the status if it is good.

If status is bad the GetResponse call throws an exception. The
WebException thrown has a Status property you can use to determine
why.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top