WebException 构造函数

定义

初始化 WebException 类的新实例。

重载

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

Source:
WebException.cs
Source:
WebException.cs
Source:
WebException.cs

初始化 WebException 类的新实例。

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

示例

以下示例引发默认 WebException的 。

try
{
   // A 'Socket' object has been created.
   Socket^ httpSocket = gcnew 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 = gcnew 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 gcnew 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 属性初始化为 null。 将 Status 属性初始化为 RequestCanceled

适用于

WebException(String)

Source:
WebException.cs
Source:
WebException.cs
Source:
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

错误消息的文本。

示例

以下示例通过指定错误消息来引发 WebException

try
{
   // A 'Socket' object has been created.
   Socket^ httpSocket = gcnew 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 = gcnew IPEndPoint( serverAddress, 80 );
   httpSocket->Connect( endPoint );
   Console::WriteLine( "Connection created successfully" );
   httpSocket->Close();
}
catch ( SocketException^ e ) 
{
   Console::WriteLine( "\nException thrown.\nThe Original Message is: {0}", e->Message );
   
   // Throw the 'WebException' object with a message string specific to the situation.
   throw gcnew WebException( "Unable to locate the Server with 'www.contoso.com' Uri." );
}
 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 属性初始化为 null。 将 Status 属性初始化为 RequestCanceled

另请参阅

适用于

WebException(SerializationInfo, StreamingContext)

Source:
WebException.cs
Source:
WebException.cs
Source:
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);
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);
new System.Net.WebException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Net.WebException
[<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
Protected Sub New (serializationInfo As SerializationInfo, streamingContext As StreamingContext)

参数

serializationInfo
SerializationInfo

SerializationInfo,包含序列化新的 WebException 所需的信息。

streamingContext
StreamingContext

StreamingContext,它包含与新的 WebException 关联的序列化流的源。

属性

注解

此构造函数实现 ISerializable 类的 WebException 接口。

另请参阅

适用于

WebException(String, WebExceptionStatus)

Source:
WebException.cs
Source:
WebException.cs
Source:
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

错误消息的文本。

示例

以下示例通过指定错误消息和 WebExceptionStatus来引发 WebException

try
{
   // A 'Socket' object has been created.
   Socket^ httpSocket = gcnew 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 = gcnew IPEndPoint( serverAddress, 80 );
   httpSocket->Connect( endPoint );
   Console::WriteLine( "Connection created successfully" );
   httpSocket->Close();
}
catch ( SocketException^ e ) 
{
   Console::WriteLine( "\nException thrown.\nThe Original Message is: {0}", e->Message );
   // Throw the 'WebException' object with a message string and message status specific to the situation.
   throw gcnew WebException( "Unable to locate the Server with 'www.contoso.com' Uri.", WebExceptionStatus::NameResolutionFailure );
}

 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)

Source:
WebException.cs
Source:
WebException.cs
Source:
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

嵌套异常。

示例

以下示例通过指定错误消息和嵌套异常来引发 WebException

try
{
   // A 'Socket' object has been created.
   Socket^ httpSocket = gcnew 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 = gcnew IPEndPoint( serverAddress, 80 );
   httpSocket->Connect( endPoint );
   Console::WriteLine( "Connection created successfully" );
   httpSocket->Close();
}
catch ( SocketException^ e ) 
{
   Console::WriteLine( "\nException thrown.\nThe Original Message is: {0}", e->Message );
   //  Throw the 'WebException' object with a message string specific to the situation;
   //  and the 'InnerException' that actually led to this exception.
   throw gcnew WebException( "Unable to locate the Server with 'www.contoso.com' Uri.", e );
}

   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 属性初始化为 null。 将 Status 属性初始化为 RequestCanceled

另请参阅

适用于

WebException(String, Exception, WebExceptionStatus, WebResponse)

Source:
WebException.cs
Source:
WebException.cs
Source:
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

嵌套异常。

response
WebResponse

包含来自远程主机的响应的 WebResponse 实例。

示例

以下示例通过指定错误消息和 WebExceptionStatus来引发 WebException

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

connectSocket->Close();
      // 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 设置为 的值 messageInnerException 将 属性设置为 的值 innerException,将 属性设置为 的值, Status 将 属性设置为 的值 status,并将 Response 属性设置为 的值 response。 如果 messagenull,则 Message 属性初始化为系统提供的消息。

适用于