导出 (0) 打印
全部展开

WebException.Status 属性

获取响应的状态。

命名空间:System.Net
程序集:System(在 system.dll 中)

public WebExceptionStatus Status { get; }
/** @property */
public WebExceptionStatus get_Status ()

public function get Status () : WebExceptionStatus

属性值

WebExceptionStatus 值之一。

Status 属性指示 WebException 的原因。

Status 的值为 WebExceptionStatus 值之一。

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

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.
    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.get_Message());
    if (e.get_Status().Equals(WebExceptionStatus.ProtocolError)) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)(e.
            get_Response())).get_StatusCode());
        Console.WriteLine("Status Description : {0}",((HttpWebResponse)(
            e.get_Response())).get_StatusDescription());
    }
}

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、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

社区附加资源

添加
显示:
© 2014 Microsoft