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

HttpStatusCode Enumerations...

The StatusCode is returned as an HttpWebResponse Member Name
such as OK, Moved, NotFound and so on.

Does anybody know how to get an actual HTTP return code?

--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET cs*********@REMOVETHISTEXTmetromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
Nov 18 '05 #1
4 1919
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in
news:Or**************@TK2MSFTNGP09.phx.gbl:
The StatusCode is returned as an HttpWebResponse Member Name
such as OK, Moved, NotFound and so on.

Does anybody know how to get an actual HTTP return code?


From the help entry for HttpStatusCode:

"The HttpStatusCode enumeration contains the values of the status
codes defined in RFC 2616 for HTTP 1.1."

HttpStatusCode is an enumeration, so it's already a number. An
enumeration's default numeric type is Int32 (int in C#). Try:

using System;
using System.Net;

namespace ExampleNamespace
{
public class ExampleClass
{
[STAThread]
public static void Main()
{
HttpWebRequest httpReq =
(HttpWebRequest) WebRequest.Create("http://www.yahoo.com");
httpReq.AllowAutoRedirect = false;

HttpWebResponse httpRes =
(HttpWebResponse) httpReq.GetResponse();

Console.WriteLine("httpRes.StatusCode name = {0}",
httpRes.StatusCode) ;
Console.WriteLine("httpRes.StatusCode value = {0}",
(int) httpRes.StatusCode) ;

// Close the response.
httpRes.Close();
}
}
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 18 '05 #2
Thanks for the copy and pasted code Chris. ;-) I used a similar snippet
and read the same statement regarding the enumeration...

Using the remote MSDN copy of what we have locally...
http://msdn.microsoft.com/library/de...classtopic.asp
To make the long story short I was not casting the StatusCode
property to an int. How would I know? Am I to understand that
all enums return an integer type?

Here's my (curious) code that now returns desired results...

HttpWebRequest httpReq =
(HttpWebRequest)WebRequest.Create("http://metromilwaukee.com");
httpReq.AllowAutoRedirect = false;

HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();

lblResponseValue.Text = "<b>Member Name: </b>"
+ httpRes.StatusCode + "<br
/>"
+ "<b>Status Code: </b>"
+ (int) httpRes.StatusCode
+"<br />"
+ "<b>Last Modified: </b>"
+ httpRes.LastModified;

// Note the curiosity regarding no requirement to use the ToString( )
method
nor cast the same property to an int unless I would have somehow known
to
do so. These circumstantial uses of type are keeping me confused.

<%= Clinton Gallagher


"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in
message news:Xn**********************************@207.46.2 48.16...
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote in
news:Or**************@TK2MSFTNGP09.phx.gbl:
The StatusCode is returned as an HttpWebResponse Member Name
such as OK, Moved, NotFound and so on.

Does anybody know how to get an actual HTTP return code?


From the help entry for HttpStatusCode:

"The HttpStatusCode enumeration contains the values of the status
codes defined in RFC 2616 for HTTP 1.1."

HttpStatusCode is an enumeration, so it's already a number. An
enumeration's default numeric type is Int32 (int in C#). Try:

using System;
using System.Net;

namespace ExampleNamespace
{
public class ExampleClass
{
[STAThread]
public static void Main()
{
HttpWebRequest httpReq =
(HttpWebRequest) WebRequest.Create("http://www.yahoo.com");
httpReq.AllowAutoRedirect = false;

HttpWebResponse httpRes =
(HttpWebResponse) httpReq.GetResponse();

Console.WriteLine("httpRes.StatusCode name = {0}",
httpRes.StatusCode) ;
Console.WriteLine("httpRes.StatusCode value = {0}",
(int) httpRes.StatusCode) ;

// Close the response.
httpRes.Close();
}
}
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 18 '05 #3
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote
in news:eV**************@TK2MSFTNGP11.phx.gbl:
Thanks for the copy and pasted code Chris. ;-) I used a similar
snippet and read the same statement regarding the enumeration...

Using the remote MSDN copy of what we have locally...
http://msdn.microsoft.com/library/de...library/en-us/
cpref/html/frlrfsystemnethttpstatuscodeclasstopic.asp

To make the long story short I was not casting the StatusCode
property to an int. How would I know? Am I to understand that
all enums return an integer type?
Clinton,

Int32 is the default type for enums. You can specify any other
integral type (except System.Char) as the type for an enum.

Enums can be a little confusing. A fair amount of casting has to be
used when accessing an enum value. There's also one way to create an
enum, and another way to access its properties and methods.

Creating an enum is done via the enum keyword:

// Using the default System.Int32 (C# "int) type:
public enum Enum1 { Value1 = 1, Value2 = 2 }

// Using the System.Byte (C# "byte") type:
public enum Enum2 : byte { Value1 = 128, Value2 = 138 }

Enum instances can be accessed using the methods and
properties defined in the System.Enum class. The help entries for
this class have several examples.

// Note the curiosity regarding no requirement to use the
ToString( ) method
nor cast the same property to an int unless I would have somehow
known to
do so. These circumstantial uses of type are keeping me
confused.


The C# Language Reference, while dry at times, is invaluable in
learning all of the nuances and quirks of C#. Section 7.7.4 covers
the addition operator (+). For string addition where only one of the
operands is a string, the compiler will call the .ToString() method
of the non-string operand. You can see this in action by overriding
the .ToString() method of a simple class:
using System;

namespace ExampleNamespace
{
public class ToStringClass
{
// By default, ToString() returns the fully
// qualified name of its type. Comment out this
// ToString() method to see what would otherwise be printed
// by the code in the Main() method...

public override string ToString()
{
return "42";
}
}

public class ExampleClass
{
[STAThread]
public static void Main()
{
ToStringClass tsc = new ToStringClass();

string msg =
"The answer to life, the universe and " +
"everything is " + tsc;

Console.WriteLine(msg) ;
}
}
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 18 '05 #4

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in
message news:Xn**********************************@207.46.2 48.16...
"clintonG" <csgallagher@RE************@metromilwaukee.com> wrote
in news:eV**************@TK2MSFTNGP11.phx.gbl:
Thanks for the copy and pasted code Chris. ;-) I used a similar
snippet and read the same statement regarding the enumeration...

Using the remote MSDN copy of what we have locally...
http://msdn.microsoft.com/library/de...library/en-us/
cpref/html/frlrfsystemnethttpstatuscodeclasstopic.asp

To make the long story short I was not casting the StatusCode
property to an int. How would I know? Am I to understand that
all enums return an integer type?
Clinton,

Int32 is the default type for enums. You can specify any other
integral type (except System.Char) as the type for an enum.

Enums can be a little confusing. A fair amount of casting has to be
used when accessing an enum value. There's also one way to create an
enum, and another way to access its properties and methods.

Creating an enum is done via the enum keyword:

// Using the default System.Int32 (C# "int) type:
public enum Enum1 { Value1 = 1, Value2 = 2 }

// Using the System.Byte (C# "byte") type:
public enum Enum2 : byte { Value1 = 128, Value2 = 138 }

Enum instances can be accessed using the methods and
properties defined in the System.Enum class. The help entries for
this class have several examples.

// Note the curiosity regarding no requirement to use the
ToString( ) method
nor cast the same property to an int unless I would have somehow
known to
do so. These circumstantial uses of type are keeping me
confused.


The C# Language Reference, while dry at times, is invaluable in
learning all of the nuances and quirks of C#. Section 7.7.4 covers
the addition operator (+). For string addition where only one of the

operands is a string, the compiler will call the .ToString() method
of the non-string operand. You can see this in action by overriding
the .ToString() method of a simple class:
using System;

namespace ExampleNamespace
{
public class ToStringClass
{
// By default, ToString() returns the fully
// qualified name of its type. Comment out this
// ToString() method to see what would otherwise be printed
// by the code in the Main() method...

public override string ToString()
{
return "42";
}
}

public class ExampleClass
{
[STAThread]
public static void Main()
{
ToStringClass tsc = new ToStringClass();

string msg =
"The answer to life, the universe and " +
"everything is " + tsc;

Console.WriteLine(msg) ;
}
}
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/


Thanks so much for the lucid explanations Chris. I will be making an
effort to make a thorough study of enums as well as study of the
C# language reference with regard to its type casting behaviors.

<%= Clinton
Nov 18 '05 #5

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

Similar topics

1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.