Handling Errors

The WebRequest and WebResponse classes throw both system exceptions (such as ArgumentException) and Web-specific exceptions (which are WebExceptions thrown by the GetResponse method).

Each WebException includes a Status property that contains a value from the WebExceptionStatus enumeration. You can examine the Status property to determine the error that occurred and take the proper steps to resolve the error.

The following table describes the possible values for the Status property.

Status

Description

ConnectFailure

The remote service could not be contacted at the transport level.

ConnectionClosed

The connection was closed prematurely.

KeepAliveFailure

The server closed a connection made with the Keep-alive header set.

NameResolutionFailure

The name service could not resolve the host name.

ProtocolError

The response received from the server was complete but indicated an error at the protocol level.

ReceiveFailure

A complete response was not received from the remote server.

RequestCanceled

The request was canceled.

SecureChannelFailure

An error occurred in a secure channel link.

SendFailure

A complete request could not be sent to the remote server.

ServerProtocolViolation

The server response was not a valid HTTP response.

Success

No error was encountered.

Timeout

No response was received within the time-out set for the request.

TrustFailure

A server certificate could not be validated.

MessageLengthLimitExceeded

A message was received that exceeded the specified limit when sending a request or receiving a response from the server.

Pending

An internal asynchronous request is pending.

PipelineFailure

This value supports the .NET Framework infrastructure and is not intended to be used directly in your code.

ProxyNameResolutionFailure

The name resolver service could not resolve the proxy host name.

UnknownError

An exception of unknown type has occurred.

When the Status property is WebExceptionStatus.ProtocolError, a WebResponse that contains the response from the server is available. You can examine this response to determine the actual source of the protocol error.

The following example shows how to catch a WebException.

try 
{
    // Create a request instance.
    WebRequest myRequest = 
    WebRequest.Create("https://www.contoso.com");
    // Get the response.
    WebResponse myResponse = myRequest.GetResponse();
    //Get a readable stream from the server. 
    Stream sr = myResponse.GetResponseStream();
                     
    //Read from the stream and write any data to the console.
    bytesread = sr.Read( myBuffer, 0, length);
    while( bytesread > 0 ) 
    {
        for (int i=0; i<bytesread; i+) {
            Console.Write( "{0}", myBuffer[i]);
        }
        Console.WriteLine();
        bytesread = sr.Read( myBuffer, 0, length);
    }
    sr.Close();
    myResponse.Close();
}
catch (WebException webExcp) 
{
    // If you reach this point, an exception has been caught.
    Console.WriteLine("A WebException has been caught.");
    // Write out the WebException message.
    Console.WriteLine(webExcp.ToString());
    // Get the WebException status code.
    WebExceptionStatus status =  webExcp.Status;
    // If status is WebExceptionStatus.ProtocolError, 
    //   there has been a protocol error and a WebResponse 
    //   should exist. Display the protocol error.
    if (status == WebExceptionStatus.ProtocolError) {
        Console.Write("The server returned protocol error ");
        // Get HttpWebResponse so that you can check the HTTP status code.
        HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response;
        Console.WriteLine((int)httpResponse.StatusCode + " - "
           + httpResponse.StatusCode);
    }
}
catch (Exception e) 
{
    // Code to catch other exceptions goes here.
}
Try
    ' Create a request instance.
    Dim myRequest As WebRequest = WebRequest.Create("https://www.contoso.com")
    ' Get the response.
    Dim myResponse As WebResponse = myRequest.GetResponse()
    'Get a readable stream from the server. 
    Dim sr As Stream = myResponse.GetResponseStream()

    Dim i As Integer    
    'Read from the stream and write any data to the console.
    bytesread = sr.Read(myBuffer, 0, length)
    While bytesread > 0
        For i = 0 To bytesread - 1
            Console.Write("{0}", myBuffer(i))
        Next i
        Console.WriteLine()
        bytesread = sr.Read(myBuffer, 0, length)
    End While
    sr.Close()
    myResponse.Close()
Catch webExcp As WebException
    ' If you reach this point, an exception has been caught.
    Console.WriteLine("A WebException has been caught.")
    ' Write out the WebException message.
    Console.WriteLine(webExcp.ToString())
    ' Get the WebException status code.
    Dim status As WebExceptionStatus = webExcp.Status
    ' If status is WebExceptionStatus.ProtocolError, 
    '   there has been a protocol error and a WebResponse 
    '   should exist. Display the protocol error.
    If status = WebExceptionStatus.ProtocolError Then
        Console.Write("The server returned protocol error ")
        ' Get HttpWebResponse so that you can check the HTTP status code.
        Dim httpResponse As HttpWebResponse = _
           CType(webExcp.Response, HttpWebResponse)
        Console.WriteLine(CInt(httpResponse.StatusCode).ToString() & _
           " - " & httpResponse.StatusCode.ToString())
    End If
Catch e As Exception
    ' Code to catch other exceptions goes here.
End Try

Applications that use the Socket class throw SocketExceptions when errors occur on the Windows socket. The TCPClient, TCPListener, and UDPClient classes are built on top of the Socket class and throw SocketExceptions as well.

When a SocketException is thrown, the SocketException class sets the ErrorCode property to the last operating system socket error that occurred. For more information about socket error codes, see the Winsock 2.0 API error code documentation in MSDN.

See Also

Concepts

Requesting Data

Other Resources

Exception Handling Fundamentals