共用方式為


WebException 建構函式

定義

初始化 WebException 類別的新執行個體。

多載

名稱 Description
WebException()

初始化 WebException 類別的新執行個體。

WebException(String)

初始化該類別的新實例 WebException ,並以指定的錯誤訊息。

WebException(SerializationInfo, StreamingContext)
已淘汰.

從指定的 SerializationInfoStreamingContext 實例中初始化該類別的新實例WebException

WebException(String, WebExceptionStatus)

初始化一個類別的新實例 WebException ,並以指定的錯誤訊息與狀態。

WebException(String, Exception)

初始化類別的新實例 WebException ,並以指定的錯誤訊息與巢狀例外。

WebException(String, Exception, WebExceptionStatus, WebResponse)

初始化該類別的新實例 WebException ,並包含指定的錯誤訊息、巢狀例外、狀態與回應。

WebException()

來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs

初始化 WebException 類別的新執行個體。

public:
 WebException();
public WebException();
Public Sub New ()

範例

以下範例拋出預設 WebException值 。


 try   
 {
     // A 'Socket' object has been created.
     Socket httpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

     // The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
     
     IPHostEntry hostEntry = Dns.Resolve("http://www.contoso.com");

     IPAddress serverAddress = hostEntry.AddressList[0];
     IPEndPoint endPoint = new IPEndPoint(serverAddress, 80);
     httpSocket.Connect(endPoint);
     Console.WriteLine("Connection created successfully");
     httpSocket.Close();
  }
catch(SocketException e)
  {
  String exp = e.Message;	
  // Throw the WebException with no parameters.
     throw new WebException();
  }
Try
    ' A 'Socket' object has been created.
    Dim httpSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    
    ' The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
     ' which leads to the 'SocketException' exception. 
    
    Dim hostEntry As IPHostEntry = Dns.Resolve("http://www.contoso.com")
    
    Dim serverAddress As IPAddress = hostEntry.AddressList(0)
    Dim endPoint As New IPEndPoint(serverAddress, 80)
    httpSocket.Connect(endPoint)
    Console.WriteLine("Connection created successfully")
    httpSocket.Close()
Catch e As SocketException
    Dim exp As [String] = e.Message
    ' Throw the WebException with no parameters.
    Throw New WebException()
End Try

備註

無參數建構子會初始化該 WebException 類別的新實例。 該 Message 屬性會被初始化為系統提供的訊息,描述錯誤。 此訊息考量了當前系統文化。 InnerExceptionResponse屬性初始化為 nullStatus 屬性會初始化為 RequestCanceled

適用於

WebException(String)

來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs

初始化該類別的新實例 WebException ,並以指定的錯誤訊息。

public:
 WebException(System::String ^ message);
public WebException(string message);
public WebException(string? message);
new System.Net.WebException : string -> System.Net.WebException
Public Sub New (message As String)

參數

message
String

錯誤訊息的文字。

範例

以下範例透過指定錯誤訊息來拋出 a WebException

 try
{
     // A 'Socket' object has been created.
     Socket httpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

     // The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
     IPHostEntry hostEntry = Dns.Resolve(connectUri);

     IPAddress serverAddress = hostEntry.AddressList[0];
     IPEndPoint endPoint = new IPEndPoint(serverAddress, 80);
     httpSocket.Connect(endPoint);
     Console.WriteLine("Connection created successfully");
     httpSocket.Close();
  }
catch(SocketException e)
  {		     
     Console.WriteLine("\nException thrown.\nThe Original Message is: "+e.Message);
     // Throw the 'WebException' object with a message string specific to the situation.
     throw new WebException("Unable to locate the Server with 'www.contoso.com' Uri.");
  }

Try
    ' A 'Socket' object has been created.
    Dim httpSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    
    ' The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
     
    Dim hostEntry As IPHostEntry = Dns.Resolve(connectUri)
    
    Dim serverAddress As IPAddress = hostEntry.AddressList(0)
    Dim endPoint As New IPEndPoint(serverAddress, 80)
    httpSocket.Connect(endPoint)
    Console.WriteLine("Connection created successfully")
    httpSocket.Close()
Catch e As SocketException
    Console.WriteLine((ControlChars.Cr + "Exception thrown." + ControlChars.Cr + "The Original Message is: " + e.Message))
    ' Throw the 'WebException' object with a message string specific to the situation.
    Throw New WebException("Unable to locate the Server with 'www.contoso.com' Uri.")
End Try

備註

WebException 實例初始化時,屬性 Message 設定為 的 message值。 若 messagenull,則 Message 該屬性初始化為系統提供的訊息。 InnerExceptionResponse屬性初始化為 nullStatus 屬性會初始化為 RequestCanceled

另請參閱

適用於

WebException(SerializationInfo, StreamingContext)

來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs

警告

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

從指定的 SerializationInfoStreamingContext 實例中初始化該類別的新實例WebException

protected:
 WebException(System::Runtime::Serialization::SerializationInfo ^ serializationInfo, System::Runtime::Serialization::StreamingContext streamingContext);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected WebException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext);
protected WebException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext);
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Net.WebException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Net.WebException
new System.Net.WebException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Net.WebException
Protected Sub New (serializationInfo As SerializationInfo, streamingContext As StreamingContext)

參數

serializationInfo
SerializationInfo

SerializationInfo A 包含序列化新 WebException. 所需的資訊。

streamingContext
StreamingContext

A StreamingContext 包含與新 WebException關聯的序列化串流來源。

屬性

備註

此建構子實 ISerializable 作該類別的 WebException 介面。

另請參閱

適用於

WebException(String, WebExceptionStatus)

來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs

初始化一個類別的新實例 WebException ,並以指定的錯誤訊息與狀態。

public:
 WebException(System::String ^ message, System::Net::WebExceptionStatus status);
public WebException(string message, System.Net.WebExceptionStatus status);
public WebException(string? message, System.Net.WebExceptionStatus status);
new System.Net.WebException : string * System.Net.WebExceptionStatus -> System.Net.WebException
Public Sub New (message As String, status As WebExceptionStatus)

參數

message
String

錯誤訊息的文字。

status
WebExceptionStatus

這是其中一項 WebExceptionStatus 價值。

範例

以下範例透過指定錯誤訊息 和 WebExceptionStatus來拋出 a WebException


 try
{
       // A 'Socket' object has been created.
       Socket httpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

      // The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
  
      IPHostEntry hostEntry = Dns.Resolve("http://www.contoso.com");

      IPAddress serverAddress = hostEntry.AddressList[0];
      IPEndPoint endPoint = new IPEndPoint(serverAddress, 80);
      httpSocket.Connect(endPoint);
      Console.WriteLine("Connection created successfully");
      httpSocket.Close();
   }
catch(SocketException e)
  {
      Console.WriteLine("\nException thrown.\nThe Original Message is: "+e.Message);
      // Throw the 'WebException' object with a message string and message status specific to the situation.
      throw new WebException("Unable to locate the Server with 'www.contoso.com' Uri.",WebExceptionStatus.NameResolutionFailure);
  }

Try
    ' A 'Socket' object has been created.
    Dim httpSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    
    ' The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
     
    Dim hostEntry As IPHostEntry = Dns.Resolve(connectUri)
    
    Dim serverAddress As IPAddress = hostEntry.AddressList(0)
    Dim endPoint As New IPEndPoint(serverAddress, 80)
    httpSocket.Connect(endPoint)
    Console.WriteLine("Connection created successfully")
    httpSocket.Close()
Catch e As SocketException
    Console.WriteLine((ControlChars.Cr + "Exception thrown." + ControlChars.Cr + "The Original Message is: " + e.Message))
    ' Throw the 'WebException' object with a message string and message status specific to the situation.
    Throw New WebException("Unable to locate the Server with 'www.contoso.com' Uri.", WebExceptionStatus.NameResolutionFailure)
End Try

備註

WebException 實例初始化時,屬性 Message 設為 的 message 值, Status 屬性設定為 的 status值。 若 messagenull,則 Message 該屬性初始化為系統提供的訊息。 InnerExceptionResponse屬性初始化為 null

適用於

WebException(String, Exception)

來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs

初始化類別的新實例 WebException ,並以指定的錯誤訊息與巢狀例外。

public:
 WebException(System::String ^ message, Exception ^ inner);
public:
 WebException(System::String ^ message, Exception ^ innerException);
public WebException(string message, Exception inner);
public WebException(string? message, Exception? innerException);
public WebException(string message, Exception innerException);
new System.Net.WebException : string * Exception -> System.Net.WebException
new System.Net.WebException : string * Exception -> System.Net.WebException
Public Sub New (message As String, inner As Exception)
Public Sub New (message As String, innerException As Exception)

參數

message
String

錯誤訊息的文字。

innerinnerException
Exception

一個巢狀例外。

範例

以下範例透過指定錯誤訊息與巢狀例外來拋出 a WebException


   try
   {
          // A 'Socket' object has been created.
          Socket httpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

          // The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
       
          IPHostEntry hostEntry = Dns.Resolve(connectUri);

          IPAddress serverAddress = hostEntry.AddressList[0];
          IPEndPoint endPoint = new IPEndPoint(serverAddress, 80);
          httpSocket.Connect(endPoint);
          Console.WriteLine("Connection created successfully");
          httpSocket.Close();		 
    }
  catch(SocketException e)
{ 
       Console.WriteLine("\nException thrown.\nThe Original Message is: "+e.Message);
       //  Throw the 'WebException' object with a message string specific to the situation; 
          //  and the 'InnerException' which actually lead to this exception.
          throw new WebException("Unable to locate the Server with 'www.contoso.com' Uri.",e);
    }

Try
    ' A 'Socket' object has been created.
    Dim httpSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    
    ' The IPaddress of the unknown uri is resolved using the 'Dns.Resolve' method. 
     Dim hostEntry As IPHostEntry = Dns.Resolve(connectUri)
    
    Dim serverAddress As IPAddress = hostEntry.AddressList(0)
    Dim endPoint As New IPEndPoint(serverAddress, 80)
    httpSocket.Connect(endPoint)
    Console.WriteLine("Connection created successfully")
    httpSocket.Close()
Catch e As SocketException
    Console.WriteLine((ControlChars.Cr + "Exception thrown." + ControlChars.Cr + "The Original Message is: " + e.Message))
    ' Throw the 'WebException' object with a message string specific to the situation. 
     ' and the 'InnerException' which actually lead to this exception.
    Throw New WebException("Unable to locate the Server with 'www.contoso.com' Uri.", e)
End Try

備註

WebException 實例初始化時,屬性 Message 設為 的 message 值, InnerException 屬性設定為 的 innerException值。 若 messagenull,則 Message 該屬性初始化為系統提供的訊息。 InnerExceptionResponse屬性初始化為 nullStatus 屬性會初始化為 RequestCanceled

另請參閱

適用於

WebException(String, Exception, WebExceptionStatus, WebResponse)

來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs
來源:
WebException.cs

初始化該類別的新實例 WebException ,並包含指定的錯誤訊息、巢狀例外、狀態與回應。

public:
 WebException(System::String ^ message, Exception ^ inner, System::Net::WebExceptionStatus status, System::Net::WebResponse ^ response);
public:
 WebException(System::String ^ message, Exception ^ innerException, System::Net::WebExceptionStatus status, System::Net::WebResponse ^ response);
public WebException(string message, Exception inner, System.Net.WebExceptionStatus status, System.Net.WebResponse response);
public WebException(string? message, Exception? innerException, System.Net.WebExceptionStatus status, System.Net.WebResponse? response);
public WebException(string message, Exception innerException, System.Net.WebExceptionStatus status, System.Net.WebResponse response);
new System.Net.WebException : string * Exception * System.Net.WebExceptionStatus * System.Net.WebResponse -> System.Net.WebException
new System.Net.WebException : string * Exception * System.Net.WebExceptionStatus * System.Net.WebResponse -> System.Net.WebException
Public Sub New (message As String, inner As Exception, status As WebExceptionStatus, response As WebResponse)
Public Sub New (message As String, innerException As Exception, status As WebExceptionStatus, response As WebResponse)

參數

message
String

錯誤訊息的文字。

innerinnerException
Exception

一個巢狀例外。

status
WebExceptionStatus

這是其中一項 WebExceptionStatus 價值。

response
WebResponse

一個 WebResponse 包含遠端主機回應的實例。

範例

以下範例透過指定錯誤訊息 和 WebExceptionStatus來拋出 a WebException

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

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

備註

WebException 實例初始化時,屬性 Message 設定為 的 message值, InnerException 屬性設定為 的值 innerExceptionStatus 屬性設定為 的值 status,屬性 Response 設定為 的值 response。 若 messagenull,則 Message 該屬性初始化為系統提供的訊息。

適用於