Поделиться через


WebException Constructor

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the WebException class.

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

Syntax

'Declaration
Public Sub New
public WebException()

Remarks

The default constructor initializes a new instance of the WebException class. The Message property is initialized to a system-supplied message that describes the error. This message takes into account the current system culture. The InnerException and Response properties are initialized to nulla null reference (Nothing in Visual Basic). The Status property is initialized to RequestCanceled.

Examples

The following example throws a default WebException.

  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("https://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();
                }
             } 
             // socket operation not a connect!  
         else
            throw new Exception("Invalid operation completed");
    }
  }

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.