共用方式為


WebException.Status 屬性

定義

取得回應的狀態。

public:
 property System::Net::WebExceptionStatus Status { System::Net::WebExceptionStatus get(); };
public System.Net.WebExceptionStatus Status { get; }
member this.Status : System.Net.WebExceptionStatus
Public ReadOnly Property Status As WebExceptionStatus

屬性值

其中一個 WebExceptionStatus 值。

範例

下列範例會 Status 檢查 屬性,並將列印至主控台 StatusCode 的和 StatusDescription 基礎 HttpWebResponse 實例。

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

備註

屬性 Status 表示 的原因 WebException

的值 Status 是其中 WebExceptionStatus 一個值。

警告

錯誤 ProxyNameResolutionFailure 不會傳回 Windows 8.x 市集應用程式。

適用於

另請參閱