Retrieving HTTP Response Code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Fink

    Retrieving HTTP Response Code

    How do I retrieve the HTTP Status code (ie 202, 404, etc) below? I am
    currently receiving the response text but unable to access the response
    status code.


    string strNewValue;

    string strResponse;

    // Create the request obj

    HttpWebRequest req = (HttpWebRequest ) WebRequest.Crea te(txtURL.Text) ;

    // Set values for the request back

    req.Method = "POST";

    //req.ContentType = "applicatio n/x-www-form-urlencoded";

    strNewValue = Server.HtmlDeco de(txtRequest.V alue.ToString() );

    //req.ContentLeng th = strNewValue.Len gth;

    req.

    // Write the request

    StreamWriter stOut = new StreamWriter (req.GetRequest Stream(),
    System.Text.Enc oding.ASCII);

    stOut.Write(str NewValue);

    stOut.Close();

    // Do the request to get the response

    StreamReader stIn = new StreamReader(re q.GetResponse() .GetResponseStr eam());

    strResponse = stIn.ReadToEnd( );

    stIn.Close();

    txtResponse.Val ue = strResponse;


  • Alex Passos

    #2
    Re: Retrieving HTTP Response Code

    Hi Chris,

    HttpWebResponse rep = req.GetResponse ();
    HttpStatusCode c = rep.StatusCode;

    when getting the response you may have to deal with exceptions if the
    request is not successful so wrap it in a try-catch block.

    Alex

    "Chris Fink" <chris.fink@gma il.com> wrote in message
    news:eXs2QCTOFH A.3960@TK2MSFTN GP12.phx.gbl...[color=blue]
    > How do I retrieve the HTTP Status code (ie 202, 404, etc) below? I am
    > currently receiving the response text but unable to access the response
    > status code.
    >
    >
    > string strNewValue;
    >
    > string strResponse;
    >
    > // Create the request obj
    >
    > HttpWebRequest req = (HttpWebRequest ) WebRequest.Crea te(txtURL.Text) ;
    >
    > // Set values for the request back
    >
    > req.Method = "POST";
    >
    > //req.ContentType = "applicatio n/x-www-form-urlencoded";
    >
    > strNewValue = Server.HtmlDeco de(txtRequest.V alue.ToString() );
    >
    > //req.ContentLeng th = strNewValue.Len gth;
    >
    > req.
    >
    > // Write the request
    >
    > StreamWriter stOut = new StreamWriter (req.GetRequest Stream(),
    > System.Text.Enc oding.ASCII);
    >
    > stOut.Write(str NewValue);
    >
    > stOut.Close();
    >
    > // Do the request to get the response
    >
    > StreamReader stIn = new
    > StreamReader(re q.GetResponse() .GetResponseStr eam());
    >
    > strResponse = stIn.ReadToEnd( );
    >
    > stIn.Close();
    >
    > txtResponse.Val ue = strResponse;
    >
    >[/color]


    Comment

    • Chris Fink

      #3
      Re: Retrieving HTTP Response Code

      This works great, thank you. However, I would like to return the actual
      status code instead of the public enum HttpStatusCode representation. For
      example, I would to see 202 instead of Accepted.

      Is this possible?

      "Alex Passos" <bz@netmerlin.n ospam.com> wrote in message
      news:uTYnVMTOFH A.3336@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi Chris,
      >
      > HttpWebResponse rep = req.GetResponse ();
      > HttpStatusCode c = rep.StatusCode;
      >
      > when getting the response you may have to deal with exceptions if the
      > request is not successful so wrap it in a try-catch block.
      >
      > Alex
      >
      > "Chris Fink" <chris.fink@gma il.com> wrote in message
      > news:eXs2QCTOFH A.3960@TK2MSFTN GP12.phx.gbl...[color=green]
      > > How do I retrieve the HTTP Status code (ie 202, 404, etc) below? I am
      > > currently receiving the response text but unable to access the response
      > > status code.
      > >
      > >
      > > string strNewValue;
      > >
      > > string strResponse;
      > >
      > > // Create the request obj
      > >
      > > HttpWebRequest req = (HttpWebRequest ) WebRequest.Crea te(txtURL.Text) ;
      > >
      > > // Set values for the request back
      > >
      > > req.Method = "POST";
      > >
      > > //req.ContentType = "applicatio n/x-www-form-urlencoded";
      > >
      > > strNewValue = Server.HtmlDeco de(txtRequest.V alue.ToString() );
      > >
      > > //req.ContentLeng th = strNewValue.Len gth;
      > >
      > > req.
      > >
      > > // Write the request
      > >
      > > StreamWriter stOut = new StreamWriter (req.GetRequest Stream(),
      > > System.Text.Enc oding.ASCII);
      > >
      > > stOut.Write(str NewValue);
      > >
      > > stOut.Close();
      > >
      > > // Do the request to get the response
      > >
      > > StreamReader stIn = new
      > > StreamReader(re q.GetResponse() .GetResponseStr eam());
      > >
      > > strResponse = stIn.ReadToEnd( );
      > >
      > > stIn.Close();
      > >
      > > txtResponse.Val ue = strResponse;
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Alex Passos

        #4
        Re: Retrieving HTTP Response Code

        I have actually not tried this but I wonder if you can cast the StatusCode
        to an integer (or convert via System.Convert) . The MSDN docs mention that
        each status code maps to a numerical representation of the code:



        Alex

        "Chris Fink" <chris.fink@gma il.com> wrote in message
        news:%23awGIGUO FHA.4028@tk2msf tngp13.phx.gbl. ..[color=blue]
        > This works great, thank you. However, I would like to return the actual
        > status code instead of the public enum HttpStatusCode representation. For
        > example, I would to see 202 instead of Accepted.
        >
        > Is this possible?
        >
        > "Alex Passos" <bz@netmerlin.n ospam.com> wrote in message
        > news:uTYnVMTOFH A.3336@TK2MSFTN GP09.phx.gbl...[color=green]
        >> Hi Chris,
        >>
        >> HttpWebResponse rep = req.GetResponse ();
        >> HttpStatusCode c = rep.StatusCode;
        >>
        >> when getting the response you may have to deal with exceptions if the
        >> request is not successful so wrap it in a try-catch block.
        >>
        >> Alex
        >>
        >> "Chris Fink" <chris.fink@gma il.com> wrote in message
        >> news:eXs2QCTOFH A.3960@TK2MSFTN GP12.phx.gbl...[color=darkred]
        >> > How do I retrieve the HTTP Status code (ie 202, 404, etc) below? I am
        >> > currently receiving the response text but unable to access the response
        >> > status code.
        >> >
        >> >
        >> > string strNewValue;
        >> >
        >> > string strResponse;
        >> >
        >> > // Create the request obj
        >> >
        >> > HttpWebRequest req = (HttpWebRequest ) WebRequest.Crea te(txtURL.Text) ;
        >> >
        >> > // Set values for the request back
        >> >
        >> > req.Method = "POST";
        >> >
        >> > //req.ContentType = "applicatio n/x-www-form-urlencoded";
        >> >
        >> > strNewValue = Server.HtmlDeco de(txtRequest.V alue.ToString() );
        >> >
        >> > //req.ContentLeng th = strNewValue.Len gth;
        >> >
        >> > req.
        >> >
        >> > // Write the request
        >> >
        >> > StreamWriter stOut = new StreamWriter (req.GetRequest Stream(),
        >> > System.Text.Enc oding.ASCII);
        >> >
        >> > stOut.Write(str NewValue);
        >> >
        >> > stOut.Close();
        >> >
        >> > // Do the request to get the response
        >> >
        >> > StreamReader stIn = new
        >> > StreamReader(re q.GetResponse() .GetResponseStr eam());
        >> >
        >> > strResponse = stIn.ReadToEnd( );
        >> >
        >> > stIn.Close();
        >> >
        >> > txtResponse.Val ue = strResponse;
        >> >
        >> >[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Chris Fink

          #5
          Re: Retrieving HTTP Response Code

          Alex, thanks again, works like a charm

          :)

          "Alex Passos" <bz@netmerlin.n ospam.com> wrote in message
          news:evLlXUUOFH A.3336@TK2MSFTN GP09.phx.gbl...[color=blue]
          > I have actually not tried this but I wonder if you can cast the StatusCode
          > to an integer (or convert via System.Convert) . The MSDN docs mention that
          > each status code maps to a numerical representation of the code:
          >
          >[/color]
          http://msdn.microsoft.com/library/de...classtopic.asp[color=blue]
          >
          > Alex
          >
          > "Chris Fink" <chris.fink@gma il.com> wrote in message
          > news:%23awGIGUO FHA.4028@tk2msf tngp13.phx.gbl. ..[color=green]
          > > This works great, thank you. However, I would like to return the actual
          > > status code instead of the public enum HttpStatusCode representation.[/color][/color]
          For[color=blue][color=green]
          > > example, I would to see 202 instead of Accepted.
          > >
          > > Is this possible?
          > >
          > > "Alex Passos" <bz@netmerlin.n ospam.com> wrote in message
          > > news:uTYnVMTOFH A.3336@TK2MSFTN GP09.phx.gbl...[color=darkred]
          > >> Hi Chris,
          > >>
          > >> HttpWebResponse rep = req.GetResponse ();
          > >> HttpStatusCode c = rep.StatusCode;
          > >>
          > >> when getting the response you may have to deal with exceptions if the
          > >> request is not successful so wrap it in a try-catch block.
          > >>
          > >> Alex
          > >>
          > >> "Chris Fink" <chris.fink@gma il.com> wrote in message
          > >> news:eXs2QCTOFH A.3960@TK2MSFTN GP12.phx.gbl...
          > >> > How do I retrieve the HTTP Status code (ie 202, 404, etc) below? I[/color][/color][/color]
          am[color=blue][color=green][color=darkred]
          > >> > currently receiving the response text but unable to access the[/color][/color][/color]
          response[color=blue][color=green][color=darkred]
          > >> > status code.
          > >> >
          > >> >
          > >> > string strNewValue;
          > >> >
          > >> > string strResponse;
          > >> >
          > >> > // Create the request obj
          > >> >
          > >> > HttpWebRequest req = (HttpWebRequest ) WebRequest.Crea te(txtURL.Text) ;
          > >> >
          > >> > // Set values for the request back
          > >> >
          > >> > req.Method = "POST";
          > >> >
          > >> > //req.ContentType = "applicatio n/x-www-form-urlencoded";
          > >> >
          > >> > strNewValue = Server.HtmlDeco de(txtRequest.V alue.ToString() );
          > >> >
          > >> > //req.ContentLeng th = strNewValue.Len gth;
          > >> >
          > >> > req.
          > >> >
          > >> > // Write the request
          > >> >
          > >> > StreamWriter stOut = new StreamWriter (req.GetRequest Stream(),
          > >> > System.Text.Enc oding.ASCII);
          > >> >
          > >> > stOut.Write(str NewValue);
          > >> >
          > >> > stOut.Close();
          > >> >
          > >> > // Do the request to get the response
          > >> >
          > >> > StreamReader stIn = new
          > >> > StreamReader(re q.GetResponse() .GetResponseStr eam());
          > >> >
          > >> > strResponse = stIn.ReadToEnd( );
          > >> >
          > >> > stIn.Close();
          > >> >
          > >> > txtResponse.Val ue = strResponse;
          > >> >
          > >> >
          > >>
          > >>[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...