Freigeben über


WebException-Konstruktor (String, Exception, WebExceptionStatus, WebResponse)

Initialisiert eine neue Instanz der WebException-Klasse mit der angegebenen Fehlermeldung, der geschachtelten Ausnahme, dem Status und der Antwort.

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

Syntax

'Declaration
Public Sub New ( _
    message As String, _
    innerException As Exception, _
    status As WebExceptionStatus, _
    response As WebResponse _
)
'Usage
Dim message As String
Dim innerException As Exception
Dim status As WebExceptionStatus
Dim response As WebResponse

Dim instance As New WebException(message, innerException, status, response)
public WebException (
    string message,
    Exception innerException,
    WebExceptionStatus status,
    WebResponse response
)
public:
WebException (
    String^ message, 
    Exception^ innerException, 
    WebExceptionStatus status, 
    WebResponse^ response
)
public WebException (
    String message, 
    Exception innerException, 
    WebExceptionStatus status, 
    WebResponse response
)
public function WebException (
    message : String, 
    innerException : Exception, 
    status : WebExceptionStatus, 
    response : WebResponse
)

Parameter

  • message
    Der Text der Fehlermeldung.
  • innerException
    Eine geschachtelte Ausnahme.
  • response
    Eine WebResponse-Instanz, die die Antwort des Remotehosts enthält.

Hinweise

Die WebException-Instanz wird initialisiert, wobei die Message-Eigenschaft auf den Wert von message, die InnerException-Eigenschaft auf den Wert von innerException, die Status-Eigenschaft auf den Wert von status und die Response-Eigenschaft auf den Wert von response festgelegt werden. Wenn message NULL (Nothing in Visual Basic) ist, wird die Message-Eigenschaft mit einer vom System bereitgestellten Meldung initialisiert.

Beispiel

Im folgenden Beispiel wird eine WebException durch Angabe einer Fehlermeldung und eines WebExceptionStatus ausgelöst.

 
 ' 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()
      
 // 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.
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.get_ASCII();
String requestPage = "GET /nhjj.htm HTTP/1.1\r\nHost: " 
    + connectUri + "\r\nConnection: Close\r\n\r\n";
ubyte byteGet[] = ascii.GetBytes(requestPage);
ubyte recvBytes[] = new ubyte[256];
// Create an 'IPEndPoint' object.
IPHostEntry hostEntry = Dns.Resolve(connectUri);
IPAddress serverAddress = (IPAddress)hostEntry.get_AddressList().
    get_Item(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.get_Length(), (SocketFlags)0);
// Receive the information sent by the server.
int bytesReceived = connectSocket.Receive(recvBytes,
    recvBytes.get_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.
        get_Length(), (SocketFlags)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();

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

WebException-Klasse
WebException-Member
System.Net-Namespace