如何在getResponse中检查httpstatuscode.ok



我只是想知道如何在返回值之前检查http状态。

这是代码段

var httpResponse = await httpWebRequest.GetResponseAsync();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var responseText = streamReader.ReadToEnd();
        dynamic data = JObject.Parse(responseText);
            shorturl = data.shortLink;
    }
return shorturl;

正在考虑

的线条
if responseText.Contains(HttpStatusCode.OK)

if (httpResponse.StatusCode == HttpStatusCode.OK)
return shorturl;

将我指向正确方向的任何东西都将不胜感激!

内部httpstatuscode是枚举

public enum HttpStatusCode
{
    ...
    Moved = 301,
    OK = 200,
    Redirect = 302,
    ...
}

然后

HttpStatusCode statusCode;
//your response code
statusCode = response.StatusCode;

if ((int)statusCode == HttpStatusCode.OK)
return shorturl;

相关内容

  • 没有找到相关文章

最新更新