|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Retrieving HTTP Response Codecurrently 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; 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 Show quoteHide quote "Chris Fink" <chris.f***@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 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; > > 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? Show quoteHide quote "Alex Passos" <bz@netmerlin.nospam.com> wrote in message news:uTYnVMTOFHA.3336@TK2MSFTNGP09.phx.gbl... > 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.f***@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 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; > > > > > > 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemnethttpstatuscodeclasstopic.asp Alex Show quoteHide quote "Chris Fink" <chris.f***@gmail.com> wrote in message news:%23awGIGUOFHA.4028@tk2msftngp13.phx.gbl... > 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... >> 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.f***@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 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, thanks again, works like a charm
:) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnethttpstatuscodeclasstopic.asp"Alex Passos" <bz@netmerlin.nospam.com> wrote in message news:evLlXUUOFHA.3336@TK2MSFTNGP09.phx.gbl... > 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: > > Show quoteHide quote > > Alex > > "Chris Fink" <chris.f***@gmail.com> wrote in message > news:%23awGIGUOFHA.4028@tk2msftngp13.phx.gbl... > > 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... > >> 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.f***@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 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; > >> > > >> > > >> > >> > > > > > >
Other interesting topics
include build time in exe file
exchanging data between a win32 application (service) and a c# application (service) Control-click xpath in c# Start and exit applcation(s) using Windows Services deployment of dot net desktop project with crystal reports Form Post - Session Values Is there any tool may provide the functionity changing variable's name automaticlly through entire c Web Service - Create Excel Files message box fonts are invisible |
|||||||||||||||||||||||