WebException.Response WebException.Response WebException.Response WebException.Response Property

定義

リモート ホストが返す応答を取得します。Gets the response that the remote host returned.

public:
 property System::Net::WebResponse ^ Response { System::Net::WebResponse ^ get(); };
public System.Net.WebResponse Response { get; }
member this.Response : System.Net.WebResponse
Public ReadOnly Property Response As WebResponse

プロパティ値

インターネット リソースから応答がある場合は、インターネット リソースからのエラー応答を格納した WebResponse インスタンス。それ以外の場合は nullIf a response is available from the Internet resource, a WebResponse instance that contains the error response from an Internet resource; otherwise, null.

次の例ではStatus 、プロパティを確認し、基StatusCodeStatusDescriptionなるHttpWebResponseインスタンスのとをコンソールに出力します。The following example checks the Status property and prints to the console the StatusCode and StatusDescription of the underlying HttpWebResponse instance.

try
{
   // Create a web request for an unknown server (this raises the WebException).
   HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)(WebRequest::Create( "http://unknown.unknown.com" ));
   
   // Get the associated response for the above request.
   HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)(myHttpWebRequest->GetResponse());
   myHttpWebResponse->Close();
}
catch ( WebException^ e ) 
{
   Console::WriteLine( "This program is expected to throw WebException on successful run." +
      "\n\nException Message : " + e->Message );
   if ( e->Status == WebExceptionStatus::ProtocolError )
   {
      Console::WriteLine( "Status Code: {0}", ( (HttpWebResponse^)(e->Response) )->StatusCode );
      Console::WriteLine( "Status Description: {0}", ( (HttpWebResponse^)(e->Response) )->StatusDescription );
   }
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->Message );
}
try {
   // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
     HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("invalid site");

    // Get the associated response for the above request.
     HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();
}
catch(WebException e) {
    Console.WriteLine("This program is expected to throw WebException on successful run."+
                        "\n\nException Message :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }
}
catch(Exception e) {
    Console.WriteLine(e.Message);
}
 Try
     'Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
     Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("invalid site"), HttpWebRequest)
     
     'Get the associated response for the above request.
     Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
     myHttpWebResponse.Close()
 Catch e As WebException
     Console.WriteLine(e.Message)
     
      If e.Status = WebExceptionStatus.ProtocolError Then
         Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
         Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
     End If

Catch e As Exception
     Console.WriteLine(e.Message)
 End Try

注釈

HTTP などの一部のインターネットプロトコルでは、プロトコルレベルでエラーが発生したことを示す有効な応答が返されます。Some Internet protocols, such as HTTP, return otherwise valid responses indicating that an error has occurred at the protocol level. インターネット要求への応答によってエラーが示さWebRequest.GetResponseれたStatus場合、 WebExceptionStatus.ProtocolErrorWebResponseプロパティをに設定し、であったResponseWebExceptionプロパティにエラーメッセージを含むを提供します。破棄.When the response to an Internet request indicates an error, WebRequest.GetResponse sets the Status property to WebExceptionStatus.ProtocolError and provides the WebResponse that contains the error message in the Response property of the WebException that was thrown. アプリケーションでを調べてWebResponse 、実際のエラーを特定することができます。The application can examine the WebResponse to determine the actual error.

適用対象