次の方法で共有


WebException コンストラクタ (String, Exception, WebExceptionStatus, WebResponse)

WebException クラスの新しいインスタンスを、指定したエラー メッセージ、入れ子になった例外、ステータス、および応答を使用して初期化します。

名前空間: System.Net
アセンブリ: System (system.dll 内)

構文

'宣言
Public Sub New ( _
    message As String, _
    innerException As Exception, _
    status As WebExceptionStatus, _
    response As WebResponse _
)
'使用
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
)

パラメータ

  • message
    エラー メッセージのテキスト。
  • innerException
    入れ子になった例外。
  • response
    リモート ホストからの応答を格納する WebResponse インスタンス。

解説

WebException インスタンスは、Message プロパティを message の値、InnerException プロパティを innerException の値、Status プロパティを status の値、および Response プロパティを response の値に設定して初期化します。message が null 参照 (Visual Basic では Nothing) の場合、Message プロパティはシステム提供のメッセージに初期化されます。

使用例

エラー メッセージおよび WebExceptionStatus を指定して、WebException をスローする例を次に示します。

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

プラットフォーム

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

WebException クラス
WebException メンバ
System.Net 名前空間