This documentation is archived and is not being maintained.

WebException Constructor (String, Exception, WebExceptionStatus, WebResponse)

.NET Framework 1.1

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

[Visual Basic]
Public Sub New( _
   ByVal message As String, _
   ByVal innerException As Exception, _
   ByVal status As WebExceptionStatus, _
   ByVal response As WebResponse _
)
[C#]
public WebException(
   string message,
 Exception innerException,
 WebExceptionStatus status,
 WebResponse response
);
[C++]
public: WebException(
   String* message,
 Exception* innerException,
 WebExceptionStatus status,
 WebResponse* response
);
[JScript]
public function WebException(
   message : String,
 innerException : Exception,
 status : WebExceptionStatus,
 response : WebResponse
);

Parameters

message
The text of the error message.
innerException
A nested exception.
status
One of the WebExceptionStatus values.
response
A WebResponse instance containing the response from the remote host.

Remarks

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 response. If message is a null reference (Nothing in Visual Basic), the Message property is initialized to a system-supplied message.

Example

[Visual Basic, C#, C++] The following example throws a WebException by specifying an error message and a WebExceptionStatus.

[Visual Basic] 

        ' 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()
     


[C#] 
// Send the data. 
Encoding ASCII = Encoding.ASCII;
string requestPage = "GET /nhjj.htm HTTP/1.1\r\nHost: " + connectUri + "\r\nConnection: Close\r\n\r\n";
Byte[] byteGet = ASCII.GetBytes(requestPage);
Byte[] recvBytes = new Byte[256];

// Create an 'IPEndPoint' object.
        
IPHostEntry hostEntry = Dns.Resolve(connectUri);
IPAddress serverAddress = hostEntry.AddressList[0];
IPEndPoint endPoint = new IPEndPoint(serverAddress, 80);
       
// Create a 'Socket' object  for sending data.
Socket connectSocket = 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.
Int32 bytesReceived = connectSocket.Receive(recvBytes, recvBytes.Length, 0);
String headerString = ASCII.GetString(recvBytes, 0, bytesReceived);
            


            
            // Check whether 'status 404' is there or not in the information sent by server.
            if(headerString.IndexOf("404")!=-1)
            {     
   bytesReceived = connectSocket.Receive(recvBytes, recvBytes.Length, 0);
   MemoryStream memoryStream = new MemoryStream(recvBytes);
   getStream = (Stream) memoryStream;
        
   // Create a 'WebResponse' object
   WebResponse myWebResponse = (WebResponse) new HttpConnect(getStream);
   Exception myException = 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);
 

            }

            connectSocket.Close();    

[C++] 
// Send the data.
Encoding*  ASCII = Encoding::ASCII;
String* requestPage = String::Concat(S"GET /nhjj.htm HTTP/1.1\r\nHost: ", connectUri, S"\r\nConnection: Close\r\n\r\n");
Byte byteGet[] = ASCII->GetBytes(requestPage);
Byte recvBytes[] = new Byte[256];

// Create an 'IPEndPoint' object.
IPHostEntry* hostEntry = Dns::Resolve(connectUri);
IPAddress* serverAddress = hostEntry->AddressList[0];
IPEndPoint* endPoint = new IPEndPoint(serverAddress, 80);

// Create a 'Socket' object  for sending data.
Socket* connectSocket = 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,static_cast<SocketFlags>(0));

// Receive the information sent by the server.
Int32 bytesReceived = connectSocket->Receive(recvBytes, recvBytes->Length,static_cast<SocketFlags>(0));
String* headerString = ASCII->GetString(recvBytes, 0, bytesReceived);

// Check whether 'status 404' is there or not in the information sent by server.
if (headerString->IndexOf(S"404")!=-1) {
   bytesReceived = connectSocket->Receive(recvBytes, recvBytes->Length,static_cast<SocketFlags>(0));
   MemoryStream* memoryStream = new MemoryStream(recvBytes);
   getStream = dynamic_cast<System::IO::Stream*>(memoryStream);

   // Create a 'WebResponse' object
   WebResponse * myWebResponse = dynamic_cast<WebResponse*>(new HttpConnect(getStream));
   Exception* myException = new Exception(S"File Not found");

   // Throw the 'WebException' object with a message string, message status, InnerException and WebResponse
   throw new WebException(S"The Requested page is not found.",
      myException, WebExceptionStatus::ProtocolError, myWebResponse);
}

connectSocket->Close();

[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 | WebException Constructor Overload List

Show: