WebException.Response 属性

定义

获取远程主机返回的响应。

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

属性值

如果可从 Internet 资源获得响应,则为包含来自 Internet 资源的错误响应的 WebResponse 实例;否则为 null

示例

以下示例检查 属性,Status并将 基础HttpWebResponse实例的 和 StatusDescription 输出到控制台StatusCode

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

注解

某些 Internet 协议(如 HTTP)返回其他有效响应,指示在协议级别发生了错误。 当对 Internet 请求的响应指示错误时,WebRequest.GetResponse将 属性WebExceptionStatus.ProtocolError设置为 Status ,并提供WebResponse在引发的 的 WebException 属性中包含Response错误消息的 。 应用程序可以检查 WebResponse 以确定实际错误。

适用于