Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
WebException Constructor (String, Exception, WebExceptionStatus, WebResponse)

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
Assemblies:   System.Net.Requests (in System.Net.Requests.dll)
  System (in System.dll)

public WebException(
	string message,
	Exception innerException,
	WebExceptionStatus status,
	WebResponse response


Type: System.String

The text of the error message.

Type: System.Exception

A nested exception.

Type: System.Net.WebExceptionStatus

One of the WebExceptionStatus values.

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 null, 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 = "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.

			 // 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. 
				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);



.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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
© 2015 Microsoft