How can I get Http Status Code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Maz

    How can I get Http Status Code?

    Hi All,

    Here's the code:

    HttpWebRequest HttpWReq =
    (HttpWebRequest )WebRequest.Cre ate("http://www.asdfasdfasd fafsd.com");
    HttpWebResponse HttpWResp = (HttpWebRespons e)HttpWReq.GetR esponse();
    Response.Write( HttpWResp.Statu sCode);

    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 Skeet [C# MVP]

    #2
    Re: How can I get Http Status Code?

    Jon Maz <jonmaz@surfeu. de.no.spam> wrote:[color=blue]
    > Here's the code:
    >
    > HttpWebRequest HttpWReq =
    > (HttpWebRequest )WebRequest.Cre ate("http://www.asdfasdfasd fafsd.com");
    > HttpWebResponse HttpWResp = (HttpWebRespons e)HttpWReq.GetR esponse();
    > Response.Write( HttpWResp.Statu sCode);
    >
    > 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?[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • test account

      #3
      Re: How can I get Http Status Code?

      HttpWebResponse response;
      int code = (int) response.Status Code;

      "Jon Maz" <jonmaz@surfeu. de.no.spam> wrote in message
      news:OcE%23M797 DHA.804@tk2msft ngp13.phx.gbl.. .[color=blue]
      > Hi All,
      >
      > Here's the code:
      >
      > HttpWebRequest HttpWReq =
      > (HttpWebRequest )WebRequest.Cre ate("http://www.asdfasdfasd fafsd.com");
      > HttpWebResponse HttpWResp =[/color]
      (HttpWebRespons e)HttpWReq.GetR esponse();[color=blue]
      > Response.Write( HttpWResp.Statu sCode);
      >
      > 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
      >
      >
      >
      >[/color]


      Comment

      • Jerry III

        #4
        Re: How can I get Http Status Code?

        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" <bitchaccount@h ax0r.dyndns.org > wrote in message
        news:uSyfe8B8DH A.2752@TK2MSFTN GP09.phx.gbl...[color=blue]
        > HttpWebResponse response;
        > int code = (int) response.Status Code;
        >
        > "Jon Maz" <jonmaz@surfeu. de.no.spam> wrote in message
        > news:OcE%23M797 DHA.804@tk2msft ngp13.phx.gbl.. .[color=green]
        > > Hi All,
        > >
        > > Here's the code:
        > >
        > > HttpWebRequest HttpWReq =
        > > (HttpWebRequest )WebRequest.Cre ate("http://www.asdfasdfasd fafsd.com");
        > > HttpWebResponse HttpWResp =[/color]
        > (HttpWebRespons e)HttpWReq.GetR esponse();[color=green]
        > > Response.Write( HttpWResp.Statu sCode);
        > >
        > > 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
        > >
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Jon Maz

          #5
          Re: How can I get Http Status Code?

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

          Any ideas?

          JON



          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: How can I get Http Status Code?

            Jon Maz <jonmaz@surfeu. de.no.spam> wrote:[color=blue]
            > Exactly right - but the StatusCode property is in the HttpWebResponse , not
            > in the Exception!
            >
            > Any ideas?[/color]

            The WebException will contain a WebResponse.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Jon Maz

              #7
              Re: How can I get Http Status Code?

              But WebException.Re sponse 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


              Comment

              • Jon Maz

                #8
                Re: How can I get Http Status Code?

                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



                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: How can I get Http Status Code?

                  Jon Maz <jonmaz@surfeu. de.no.spam> wrote:[color=blue]
                  > 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 .[/color]

                  Yes - and that makes perfect sense, as an HttpWebResponse *is* a
                  WebResponse.
                  [color=blue]
                  > More importantly, I have now got it working - thanks for the help![/color]

                  Goodo.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  Working...