Retrieving HTTP Response Code

C

Chris Fink

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;
 
A

Alex Passos

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
 
C

Chris Fink

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top