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.




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


The connection was closed prematurely.


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


The name service could not resolve the host name.


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


A complete response was not received from the remote server.


The request was canceled.


An error occurred in a secure channel link.


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


The server response was not a valid HTTP response.


No error was encountered.


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


A server certificate could not be validated.


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


An internal asynchronous request is pending.


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


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


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.

    ' Create a request instance.
    Dim myRequest As WebRequest = WebRequest.Create("http://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
        bytesread = sr.Read(myBuffer, 0, length)
    End While
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.
    ' 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.