473,396 Members | 1,766 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,396 software developers and data experts.

How can I get Http Status Code?

Hi All,

Here's the code:

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
Response.Write(HttpWResp.StatusCode);

Here's what I would like it to return to me:
HTTP/1.1 400 Bad Request

And here's what it actually gives me:
The underlying connection was closed: The remote name could not be
resolved.

Any ideas how I can get the 400 code returned?

Thanks,
JON


Nov 15 '05 #1
8 31156
Jon Maz <jo****@surfeu.de.no.spam> wrote:
Here's the code:

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
Response.Write(HttpWResp.StatusCode);

Here's what I would like it to return to me:
HTTP/1.1 400 Bad Request

And here's what it actually gives me:
The underlying connection was closed: The remote name could not be
resolved.

Any ideas how I can get the 400 code returned?


Hang on a sec - does www.asdfasdfasdfafsd.com exist and return that
response, or does it genuinely not exist? If it's the latter, there
*is* no 400 code to be returned - it couldn't even send the request, so
it can't get back a response code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
HttpWebResponse response;
int code = (int) response.StatusCode;

"Jon Maz" <jo****@surfeu.de.no.spam> wrote in message
news:Oc***************@tk2msftngp13.phx.gbl...
Hi All,

Here's the code:

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); Response.Write(HttpWResp.StatusCode);

Here's what I would like it to return to me:
HTTP/1.1 400 Bad Request

And here's what it actually gives me:
The underlying connection was closed: The remote name could not be
resolved.

Any ideas how I can get the 400 code returned?

Thanks,
JON

Nov 15 '05 #3
Yeah but you can't get a response from a server that doesn't exist (or one
you can't connect to) - which is the problem here (the name cannot be
resolved)... It's just not going to happen. You need to catch the exception.

Jerry

"test account" <bi**********@hax0r.dyndns.org> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
HttpWebResponse response;
int code = (int) response.StatusCode;

"Jon Maz" <jo****@surfeu.de.no.spam> wrote in message
news:Oc***************@tk2msftngp13.phx.gbl...
Hi All,

Here's the code:

HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.asdfasdfasdfafsd.com");
HttpWebResponse HttpWResp =

(HttpWebResponse)HttpWReq.GetResponse();
Response.Write(HttpWResp.StatusCode);

Here's what I would like it to return to me:
HTTP/1.1 400 Bad Request

And here's what it actually gives me:
The underlying connection was closed: The remote name could not be
resolved.

Any ideas how I can get the 400 code returned?

Thanks,
JON


Nov 15 '05 #4
Exactly right - but the StatusCode property is in the HttpWebResponse, not
in the Exception!

Any ideas?

JON

Nov 15 '05 #5
Jon Maz <jo****@surfeu.de.no.spam> wrote:
Exactly right - but the StatusCode property is in the HttpWebResponse, not
in the Exception!

Any ideas?


The WebException will contain a WebResponse.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
But WebException.Response is of type WebResponse, which doesn't have a
StatusCode property (only HttpResponse or HttpWebResponse do).

I did just try casting it into an HttpWebResponse and getting the StatusCode
that way, but it didn't work.

Cheers,

JON
Nov 15 '05 #7
Actually although the documentation it says it's of type WebResponse, and
the IDE reacts as if it is a WebResponse, it actually seems to *be* an
HttpWebResponse.

More importantly, I have now got it working - thanks for the help!

JON

Nov 15 '05 #8
Jon Maz <jo****@surfeu.de.no.spam> wrote:
Actually although the documentation it says it's of type WebResponse, and
the IDE reacts as if it is a WebResponse, it actually seems to *be* an
HttpWebResponse.
Yes - and that makes perfect sense, as an HttpWebResponse *is* a
WebResponse.
More importantly, I have now got it working - thanks for the help!


Goodo.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9

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

Similar topics

7
by: Paul | last post by:
I thought this is more of an IE issue but i've had no joy on that group perhaps somebody here will have a clue. If i click a link to a web page embedded in Excel (97 OR 2000) i get the standard...
6
by: R. Rajesh Jeba Anbiah | last post by:
Is it necessary or is there any standard to send HTTP header status for form inputs ? Say, the user is entering invalid password in login form and now all the applications I have seen are just...
6
by: Nick Horrocks | last post by:
I have set up a custom error page for 404 errors. However the HTTP status code returned is 302 followed by 200, this causes search engines not to remove old pages from their index. How can I...
6
by: Andy Fish | last post by:
Hi, I've RTFM'd in vain for some named constants in the asp.net framework representing the HTTP error codes - the equivalent of Java's HttpServletResponse.SC_NOT_FOUND for 404 etc. All the...
0
by: DT | last post by:
I'm writing a .NET 1.1 client that invokes a method of a Web Service running on another organization's IIS 5 server. I need help understanding a fine point about HTTP, specifically in relation to...
8
by: Tim Reynolds | last post by:
Our .Net application calls a web method of aplpication 2 that resides on their Apache server. When I as a developer C#, Studios 2003, make the call to their web method from my desktop, I receive no...
2
by: inferno2000 | last post by:
Let's say if I want to send a http "Post" request to a url, and check the http status code later. How should I write the code? I have found example to use WinHttp to send "Get" request and check...
1
by: sergiotb | last post by:
Hello everyone!! I'm trying to send a image (jpg) from a C# program to a CGI perl. The problem is that the code of the client C# is not working properly...I guess. The steps to send the image are: 1)...
1
by: rpjd | last post by:
I am having a problem getting the http.status of an xmlhttprequest. The request readystate has come back as readystate 4, but then it stops without confirming the http.status. Any help appreciated....
2
by: Khai Doan | last post by:
I am using Selenium, a javascript framework for automated testing of web application. The problem occurs when there is a server side HTTP redirect. Selenium would detect that a new page is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.