By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,565 Members | 1,763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,565 IT Pros & Developers. It's quick & easy.

How can I get Http Status Code?

P: n/a
Hi All,

Here's the code:

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
Response.Write(HttpWResp.StatusCode);

Here's what I would like it to return to me:
HTTP/1.1 400 Bad Request

And here's what it actually gives me:
The underlying connection was closed: The remote name could not be
resolved.

Any ideas how I can get the 400 code returned?

Thanks,
JON


Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Jon Maz <jo****@surfeu.de.no.spam> wrote:
Here's the code:

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
Response.Write(HttpWResp.StatusCode);

Here's what I would like it to return to me:
HTTP/1.1 400 Bad Request

And here's what it actually gives me:
The underlying connection was closed: The remote name could not be
resolved.

Any ideas how I can get the 400 code returned?


Hang on a sec - does www.asdfasdfasdfafsd.com exist and return that
response, or does it genuinely not exist? If it's the latter, there
*is* no 400 code to be returned - it couldn't even send the request, so
it can't get back a response code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
HttpWebResponse response;
int code = (int) response.StatusCode;

"Jon Maz" <jo****@surfeu.de.no.spam> wrote in message
news:Oc***************@tk2msftngp13.phx.gbl...
Hi All,

Here's the code:

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); Response.Write(HttpWResp.StatusCode);

Here's what I would like it to return to me:
HTTP/1.1 400 Bad Request

And here's what it actually gives me:
The underlying connection was closed: The remote name could not be
resolved.

Any ideas how I can get the 400 code returned?

Thanks,
JON

Nov 15 '05 #3

P: n/a
Yeah but you can't get a response from a server that doesn't exist (or one
you can't connect to) - which is the problem here (the name cannot be
resolved)... It's just not going to happen. You need to catch the exception.

Jerry

"test account" <bi**********@hax0r.dyndns.org> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
HttpWebResponse response;
int code = (int) response.StatusCode;

"Jon Maz" <jo****@surfeu.de.no.spam> wrote in message
news:Oc***************@tk2msftngp13.phx.gbl...
Hi All,

Here's the code:

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp =

(HttpWebResponse)HttpWReq.GetResponse();
Response.Write(HttpWResp.StatusCode);

Here's what I would like it to return to me:
HTTP/1.1 400 Bad Request

And here's what it actually gives me:
The underlying connection was closed: The remote name could not be
resolved.

Any ideas how I can get the 400 code returned?

Thanks,
JON


Nov 15 '05 #4

P: n/a
Exactly right - but the StatusCode property is in the HttpWebResponse, not
in the Exception!

Any ideas?

JON

Nov 15 '05 #5

P: n/a
Jon Maz <jo****@surfeu.de.no.spam> wrote:
Exactly right - but the StatusCode property is in the HttpWebResponse, not
in the Exception!

Any ideas?


The WebException will contain a WebResponse.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

P: n/a
But WebException.Response is of type WebResponse, which doesn't have a
StatusCode property (only HttpResponse or HttpWebResponse do).

I did just try casting it into an HttpWebResponse and getting the StatusCode
that way, but it didn't work.

Cheers,

JON
Nov 15 '05 #7

P: n/a
Actually although the documentation it says it's of type WebResponse, and
the IDE reacts as if it is a WebResponse, it actually seems to *be* an
HttpWebResponse.

More importantly, I have now got it working - thanks for the help!

JON

Nov 15 '05 #8

P: n/a
Jon Maz <jo****@surfeu.de.no.spam> wrote:
Actually although the documentation it says it's of type WebResponse, and
the IDE reacts as if it is a WebResponse, it actually seems to *be* an
HttpWebResponse.
Yes - and that makes perfect sense, as an HttpWebResponse *is* a
WebResponse.
More importantly, I have now got it working - thanks for the help!


Goodo.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.