WebException Constructor (String)

WebException Constructor (String)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Initializes a new instance of the WebException class with the specified error message.

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

public WebException(
	string message
)

Parameters

message
Type: System.String
The text of the error message.

The WebException instance is initialized with the Message property set to the value of message. If message is null, the Message property is initialized to a system-supplied message. The InnerException and Response properties are initialized to null. The Status property is initialized to RequestCanceled.

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


  public class Example
  {
    static ManualResetEvent clientDone = new ManualResetEvent(false);

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
		    DnsEndPoint hostEntry = new DnsEndPoint("http://www.contoso.com", 80);

        // Create a socket and connect to the server
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);

        socketEventArg.RemoteEndPoint = hostEntry;
        socketEventArg.UserToken = sock;
        sock.ConnectAsync(socketEventArg);
        clientDone.WaitOne();
    }

    static void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e)
    {
         if (e.LastOperation == SocketAsyncOperation.Connect) {
           if (e.SocketError == SocketError.Success)
            {
                // Successfully connected to the server

            }
            else
            {
	   	          // Throw the WebException with no parameters.
		            throw new WebException("Unable to locate the Server with 'www.contoso.com' Uri.");
		        }
		     } 
		     // socket operation not a connect!  
         else
            throw new Exception("Invalid operation completed");
    }
  }


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft