首页 > 解决方案 > Xamarin 中的 HttpRequestException 状态码

问题描述

StatusCode属性已添加到HttpRequestException.NET 5 中。今天有什么方法可以在 Xamarin 中使用它吗?

标签: xamarin.net-5

解决方案


下面的示例将 HttpWebResponse 返回的状态与 HttpStatusCode 类的成员进行比较,以确定响应的状态。

HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("http://www.contoso.com");
httpReq.AllowAutoRedirect = false;

HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();

if (httpRes.StatusCode==HttpStatusCode.Moved)
{
    // Code for moved resources goes here.
}

// Close the response.
httpRes.Close();

推荐阅读