How can I get Http Status Code?

Discussion in 'ASP .Net' started by Jon Maz, Feb 10, 2004.

  1. Jon Maz

    Jon Maz Guest

    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
     
    Jon Maz, Feb 10, 2004
    #1
    1. Advertising

  2. Jon Maz <> 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 - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Feb 10, 2004
    #2
    1. Advertising

  3. Jon Maz

    test account Guest

    HttpWebResponse response;
    int code = (int) response.StatusCode;

    "Jon Maz" <> wrote in message
    news:OcE%...
    > 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
    >
    >
    >
    >
     
    test account, Feb 10, 2004
    #3
  4. Jon Maz

    Jerry III Guest

    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" <> wrote in message
    news:...
    > HttpWebResponse response;
    > int code = (int) response.StatusCode;
    >
    > "Jon Maz" <> wrote in message
    > news:OcE%...
    > > 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
    > >
    > >
    > >
    > >

    >
    >
     
    Jerry III, Feb 11, 2004
    #4
  5. Jon Maz

    Jon Maz Guest

    Exactly right - but the StatusCode property is in the HttpWebResponse, not
    in the Exception!

    Any ideas?

    JON
     
    Jon Maz, Feb 11, 2004
    #5
  6. Jon Maz <> 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 - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Feb 11, 2004
    #6
  7. Jon Maz

    Jon Maz Guest

    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
     
    Jon Maz, Feb 11, 2004
    #7
  8. Jon Maz

    Jon Maz Guest

    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
     
    Jon Maz, Feb 11, 2004
    #8
  9. Jon Maz <> 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 - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Feb 11, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UG1jZw==?=

    HttpWebRequest & (401) Unauthorized http status code

    =?Utf-8?B?UG1jZw==?=, Jun 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,449
    =?Utf-8?B?UG1jZw==?=
    Jun 21, 2004
  2. Nick Horrocks

    Custom not found and HTTP status code

    Nick Horrocks, Nov 8, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    488
    Joerg Jooss
    Nov 13, 2004
  3. George

    206 HTTP status code

    George, May 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    9,917
    Karl Seguin
    May 9, 2005
  4. harryos
    Replies:
    0
    Views:
    402
    harryos
    Oct 13, 2010
  5. Fred
    Replies:
    17
    Views:
    268
Loading...

Share This Page