检查GetStreamAsync状态

检查GetStreamAsync状态

本文介绍了检查GetStreamAsync状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过GetStreamAsync抓取图像,如何确定状态?

Grabbing an image via GetStreamAsync, how do I determine status?

HttpClient OpenClient = new HttpClient();
Stream firstImageStream = OpenClient.GetStreamAsync("imageUrl.jpg").Result;

有时会出现错误(通常为403或404),我只是想跳过对那些结果的处理.

Sometimes this will give an error (403 or 404 typically) and I simply want to skip processing those results.

我所能找到的全部内容是说要使用StatusCode属性或IsSuccessStatusCode,但是这些似乎只能在HttpResponseMessage上使用,该文件来自GetAsync,而没有给我提供我需要处理的Stream图像.

All I can find says to use the StatusCode property or IsSuccessStatusCode, but those seem to only work on HttpResponseMessage, which is from GetAsync, which does not give me the Stream I need to process the image.

推荐答案

流没有响应状态代码.您需要先获取HttpResponseMessage,检查状态码,然后读入流.

The stream doesn't have the response status code. You'll need to get the HttpResponseMessage first, check the status code, and then read in the stream.

HttpClient OpenClient = new HttpClient();
var response = await OpenClient.GetAsync("imageUrl.jpg");
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
    Stream stream = await response.Content.ReadAsStreamAsync();
}

这篇关于检查GetStreamAsync状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:50