473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.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;
Nov 17 '05 #1
4 30218
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" <ch********@gmail.com> wrote in message
news:eX**************@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;

Nov 17 '05 #2
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:uT**************@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" <ch********@gmail.com> wrote in message
news:eX**************@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;


Nov 17 '05 #3
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" <ch********@gmail.com> wrote in message
news:%2****************@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:uT**************@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" <ch********@gmail.com> wrote in message
news:eX**************@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;
>
>



Nov 17 '05 #4
Alex, thanks again, works like a charm

:)

"Alex Passos" <bz@netmerlin.nospam.com> wrote in message
news:ev**************@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:

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

"Chris Fink" <ch********@gmail.com> wrote in message
news:%2****************@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:uT**************@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" <ch********@gmail.com> wrote in message
news:eX**************@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;
>
>



Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonas Galvez | last post by:
Hi list, here's a question about urllib. Is it possible to simply retrieve the HTTP responde code for a given URL? I don't want to download the body of the HTTP message. I simply want to check the...
3
by: Mazin07 | last post by:
Is there any way to get the HTTP Response Code sent out by the server (ie 404, 302, etc)? Or could I just assume that the same code was sent? I know the $_SERVER array has a lot of info, but...
8
by: Jon Maz | last post by:
Hi All, Here's the code: HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com"); HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();...
2
by: | last post by:
Hello All, I am writing a web application that reads a bitmap from a file and outputing it to a HTTP response stream to return the image to the requesting client. The image file is a regular...
0
by: Christina | last post by:
Hello, I have an application that HTTP posts a document and is supposed to receive an http "response code : 400" (or 200... code depends upon the success/failure) back. My question is: 1)...
0
by: SimonDev | last post by:
Hi I've got an unusual problem I'm hoping someone could advise me on, regarding the formatting of the body of an HTTP response from a web service. We are using HTTP POST rather than SOAP for...
2
by: jl | last post by:
I have a J2ME cell phone application that via the AMS (Application Manager) delivers a status report to my server indicating whether the suite was successfully installed and identifying the reason...
1
by: Flip Rayner | last post by:
I am writing an HTTPHandler that is basically a transparent proxy / URL Mapper. I have a line in my code to return a 503: context.context.Response.StatusCode = ...
2
by: neovantage | last post by:
Hi geeks, We made a Java Servlet which takes 3 parameters and upload files from source path to remote path. The three parameters are password, source path and destination path. When we run this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.