Export (0) Print
Expand All

WebException.Status Property

Gets the status of the response.

[Visual Basic]
Public ReadOnly Property Status As WebExceptionStatus
[C#]
public WebExceptionStatus Status {get;}
[C++]
public: __property WebExceptionStatus get_Status();
[JScript]
public function get Status() : WebExceptionStatus;

Property Value

One of the WebExceptionStatus values.

Remarks

The Status property indicates the reason for the WebException.

The value of Status is one of the WebExceptionStatus values.

Example

[Visual Basic, C#, C++] The following example checks the Status property and prints to the console the StatusCode and StatusDescription of the underlying HttpWebResponse instance.

[Visual Basic] 
Try
'Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("invalid site"), HttpWebRequest)

'Get the associated response for the above request.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
myHttpWebResponse.Close()
        Catch e As WebException
Console.WriteLine(e.Message)

 If e.Status = WebExceptionStatus.ProtocolError Then
    Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
    Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
End If

[C#] 
try {
   // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
     HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("invalid site");

    // Get the associated response for the above request.
     HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();
}
catch(WebException e) {
    Console.WriteLine("This program is expected to throw WebException on successful run."+
                        "\n\nException Message :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }

[C++] 
try {
   // Create a web request for an unknown server (this raises the WebException).
   HttpWebRequest* myHttpWebRequest =
      dynamic_cast<HttpWebRequest*> (WebRequest::Create(S"http://unknown.unknown.com"));

   // Get the associated response for the above request.
   HttpWebResponse* myHttpWebResponse =
      dynamic_cast<HttpWebResponse*> (myHttpWebRequest->GetResponse());
   myHttpWebResponse->Close();
} catch (WebException* e) {
   Console::WriteLine(S"This program is expected to throw WebException on successful run. \n\nException Message : {0}", e->Message);
   if (e->Status == WebExceptionStatus::ProtocolError) {
      Console::WriteLine(S"Status Code : {0}", __box((dynamic_cast<HttpWebResponse*>(e->Response))->StatusCode));
      Console::WriteLine(S"Status Description : {0}",
         (dynamic_cast<HttpWebResponse*>(e->Response))->StatusDescription);
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

WebException Class | WebException Members | System.Net Namespace | WebExceptionStatus

Show:
© 2014 Microsoft