WebException.WebException(String, Exception, WebExceptionStatus, WebResponse) Constructor

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)

WebException (
	String^ message, 
	Exception^ innerException, 
	WebExceptionStatus status, 
	WebResponse^ response
public WebException (
	String message, 
	Exception innerException, 
	WebExceptionStatus status, 
	WebResponse response
public function WebException (
	message : String, 
	innerException : Exception, 
	status : WebExceptionStatus, 
	response : WebResponse
Not applicable.



The text of the error message.


A nested exception.


One of the WebExceptionStatus values.


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 a null reference (Nothing in Visual Basic), 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.
Encoding^ ASCII = Encoding::ASCII;
String^ requestPage = String::Concat( "GET /nhjj.htm HTTP/1.1\r\nHost: ", connectUri, "\r\nConnection: Close\r\n\r\n" );
array<Byte>^ byteGet = ASCII->GetBytes( requestPage );
array<Byte>^ recvBytes = gcnew array<Byte>(256);

// Create an 'IPEndPoint' object.

IPHostEntry^ hostEntry = Dns::Resolve( connectUri );
IPAddress^ serverAddress = hostEntry->AddressList[ 0 ];
IPEndPoint^ endPoint = gcnew IPEndPoint( serverAddress,80 );

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

// Receive the information sent by the server.
Int32 bytesReceived = connectSocket->Receive( recvBytes, recvBytes->Length, (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( "404" ) != -1 )
   bytesReceived = connectSocket->Receive( recvBytes, recvBytes->Length, (SocketFlags)(0) );
   MemoryStream^ memoryStream = gcnew MemoryStream( recvBytes );
   getStream = (System::IO::Stream^)(memoryStream);
   // Create a 'WebResponse' object
   WebResponse^ myWebResponse = (WebResponse^)(gcnew HttpConnect( getStream ));
   Exception^ myException = gcnew Exception( "File Not found" );

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


// Send the data. 
Encoding ascii = Encoding.get_ASCII();
String requestPage = "GET /nhjj.htm HTTP/1.1\r\nHost: " 
    + connectUri + "\r\nConnection: Close\r\n\r\n";
ubyte byteGet[] = ascii.GetBytes(requestPage);
ubyte recvBytes[] = new ubyte[256];
// Create an 'IPEndPoint' object.
IPHostEntry hostEntry = Dns.Resolve(connectUri);
IPAddress serverAddress = (IPAddress)hostEntry.get_AddressList().
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.
// Sent the 'requestPage' text to the host.
connectSocket.Send(byteGet, byteGet.get_Length(), (SocketFlags)0);
// Receive the information sent by the server.
int bytesReceived = connectSocket.Receive(recvBytes,
    recvBytes.get_Length(), (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("404") != -1) {
    bytesReceived = connectSocket.Receive(recvBytes, recvBytes.
        get_Length(), (SocketFlags)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);

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions