问题描述
通过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状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!