Connecting Tech Pros Worldwide Forums | Help | Site Map

Retrieving HTTP Response Code

Chris Fink
Guest
 
Posts: n/a
#1: Nov 17 '05
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.Create(txtURL.Text);

// Set values for the request back

req.Method = "POST";

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

strNewValue = Server.HtmlDecode(txtRequest.Value.ToString());

//req.ContentLength = strNewValue.Length;

req.

// Write the request

StreamWriter stOut = new StreamWriter (req.GetRequestStream(),
System.Text.Encoding.ASCII);

stOut.Write(strNewValue);

stOut.Close();

// Do the request to get the response

StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream() );

strResponse = stIn.ReadToEnd();

stIn.Close();

txtResponse.Value = strResponse;



Alex Passos
Guest
 
Posts: n/a
#2: Nov 17 '05

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@gmail.com> wrote in message
news:eXs2QCTOFHA.3960@TK2MSFTNGP12.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.Create(txtURL.Text);
>
> // Set values for the request back
>
> req.Method = "POST";
>
> //req.ContentType = "application/x-www-form-urlencoded";
>
> strNewValue = Server.HtmlDecode(txtRequest.Value.ToString());
>
> //req.ContentLength = strNewValue.Length;
>
> req.
>
> // Write the request
>
> StreamWriter stOut = new StreamWriter (req.GetRequestStream(),
> System.Text.Encoding.ASCII);
>
> stOut.Write(strNewValue);
>
> stOut.Close();
>
> // Do the request to get the response
>
> StreamReader stIn = new
> StreamReader(req.GetResponse().GetResponseStream() );
>
> strResponse = stIn.ReadToEnd();
>
> stIn.Close();
>
> txtResponse.Value = strResponse;
>
>[/color]


Chris Fink
Guest
 
Posts: n/a
#3: Nov 17 '05

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.nospam.com> wrote in message
news:uTYnVMTOFHA.3336@TK2MSFTNGP09.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@gmail.com> wrote in message
> news:eXs2QCTOFHA.3960@TK2MSFTNGP12.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.Create(txtURL.Text);
> >
> > // Set values for the request back
> >
> > req.Method = "POST";
> >
> > //req.ContentType = "application/x-www-form-urlencoded";
> >
> > strNewValue = Server.HtmlDecode(txtRequest.Value.ToString());
> >
> > //req.ContentLength = strNewValue.Length;
> >
> > req.
> >
> > // Write the request
> >
> > StreamWriter stOut = new StreamWriter (req.GetRequestStream(),
> > System.Text.Encoding.ASCII);
> >
> > stOut.Write(strNewValue);
> >
> > stOut.Close();
> >
> > // Do the request to get the response
> >
> > StreamReader stIn = new
> > StreamReader(req.GetResponse().GetResponseStream() );
> >
> > strResponse = stIn.ReadToEnd();
> >
> > stIn.Close();
> >
> > txtResponse.Value = strResponse;
> >
> >[/color]
>
>[/color]


Alex Passos
Guest
 
Posts: n/a
#4: Nov 17 '05

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:

http://msdn.microsoft.com/library/de...classtopic.asp

Alex

"Chris Fink" <chris.fink@gmail.com> wrote in message
news:%23awGIGUOFHA.4028@tk2msftngp13.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.nospam.com> wrote in message
> news:uTYnVMTOFHA.3336@TK2MSFTNGP09.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@gmail.com> wrote in message
>> news:eXs2QCTOFHA.3960@TK2MSFTNGP12.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.Create(txtURL.Text);
>> >
>> > // Set values for the request back
>> >
>> > req.Method = "POST";
>> >
>> > //req.ContentType = "application/x-www-form-urlencoded";
>> >
>> > strNewValue = Server.HtmlDecode(txtRequest.Value.ToString());
>> >
>> > //req.ContentLength = strNewValue.Length;
>> >
>> > req.
>> >
>> > // Write the request
>> >
>> > StreamWriter stOut = new StreamWriter (req.GetRequestStream(),
>> > System.Text.Encoding.ASCII);
>> >
>> > stOut.Write(strNewValue);
>> >
>> > stOut.Close();
>> >
>> > // Do the request to get the response
>> >
>> > StreamReader stIn = new
>> > StreamReader(req.GetResponse().GetResponseStream() );
>> >
>> > strResponse = stIn.ReadToEnd();
>> >
>> > stIn.Close();
>> >
>> > txtResponse.Value = strResponse;
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]


Chris Fink
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Retrieving HTTP Response Code


Alex, thanks again, works like a charm

:)

"Alex Passos" <bz@netmerlin.nospam.com> wrote in message
news:evLlXUUOFHA.3336@TK2MSFTNGP09.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@gmail.com> wrote in message
> news:%23awGIGUOFHA.4028@tk2msftngp13.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.nospam.com> wrote in message
> > news:uTYnVMTOFHA.3336@TK2MSFTNGP09.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@gmail.com> wrote in message
> >> news:eXs2QCTOFHA.3960@TK2MSFTNGP12.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.Create(txtURL.Text);
> >> >
> >> > // Set values for the request back
> >> >
> >> > req.Method = "POST";
> >> >
> >> > //req.ContentType = "application/x-www-form-urlencoded";
> >> >
> >> > strNewValue = Server.HtmlDecode(txtRequest.Value.ToString());
> >> >
> >> > //req.ContentLength = strNewValue.Length;
> >> >
> >> > req.
> >> >
> >> > // Write the request
> >> >
> >> > StreamWriter stOut = new StreamWriter (req.GetRequestStream(),
> >> > System.Text.Encoding.ASCII);
> >> >
> >> > stOut.Write(strNewValue);
> >> >
> >> > stOut.Close();
> >> >
> >> > // Do the request to get the response
> >> >
> >> > StreamReader stIn = new
> >> > StreamReader(req.GetResponse().GetResponseStream() );
> >> >
> >> > strResponse = stIn.ReadToEnd();
> >> >
> >> > stIn.Close();
> >> >
> >> > txtResponse.Value = strResponse;
> >> >
> >> >
> >>
> >>[/color]
> >
> >[/color]
>
>[/color]


Closed Thread