Export (0) Print
Expand All

WebException Constructor (String, Exception, WebExceptionStatus, WebResponse)

Initializes a new instance of the WebException class with the specified error message, nested exception, status, and response.

Namespace:  System.Net
Assembly:  System (in System.dll)

'Declaration
Public Sub New ( _
	message As String, _
	innerException As Exception, _
	status As WebExceptionStatus, _
	response As WebResponse _
)

Parameters

message
Type: System.String

The text of the error message.

innerException
Type: System.Exception

A nested exception.

status
Type: System.Net.WebExceptionStatus

One of the WebExceptionStatus values.

response
Type: System.Net.WebResponse

A WebResponse instance that contains the response from the remote host.

The WebException instance is initialized with the Message property set to the value of message, the InnerException property set to the value of innerException, the Status property set to the value of status, and the Response property set to the value of response. If message is Nothing, the Message property is initialized to a system-supplied message.

The following example throws a WebException by specifying an error message and a WebExceptionStatus.


   	  ' Send the data.  
        Dim ASCII As Encoding = Encoding.ASCII
        Dim requestPage As String = "GET /nhjj.htm HTTP/1.1" + ControlChars.Lf + ControlChars.Cr + "Host: " + connectUri + ControlChars.Lf + ControlChars.Cr + "Connection: Close" + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr
        Dim byteGet As [Byte]() = ASCII.GetBytes(requestPage)
        Dim recvBytes(256) As [Byte]

        ' Create an 'IPEndPoint' object. 
        Dim hostEntry As IPHostEntry = Dns.Resolve(connectUri)
        Dim serverAddress As IPAddress = hostEntry.AddressList(0)
        Dim endPoint As New IPEndPoint(serverAddress, 80)

        ' Create a 'Socket' object  for sending data. 
        Dim connectSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

        ' Connect to host using 'IPEndPoint' object.
        connectSocket.Connect(endPoint)

        ' Sent the 'requestPage' text to the host.
        connectSocket.Send(byteGet, byteGet.Length, 0)

        ' Receive the information sent by the server. 
        Dim bytesReceived As Int32 = connectSocket.Receive(recvBytes, recvBytes.Length, 0)
        Dim headerString As [String] = ASCII.GetString(recvBytes, 0, bytesReceived)

        ' Check whether 'status 404' is there or not in the information sent by server. 
        If headerString.IndexOf("404") <> False Then
            bytesReceived = connectSocket.Receive(recvBytes, recvBytes.Length, 0)
            Dim memoryStream As New MemoryStream(recvBytes)
            getStream = CType(memoryStream, Stream)

            ' Create a 'WebResponse' object. 
            Dim myWebResponse As WebResponse = CType(New HttpConnect(getStream), WebResponse)
            Dim myException As New Exception("File Not found")

            ' Throw the 'WebException' object with a message string, message status,InnerException and WebResponse. 
            Throw New WebException("The Requested page is not found.", myException, WebExceptionStatus.ProtocolError, myWebResponse)
        End If 

        connectSocket.Close()

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft