获得远程文件MediaType

//获得远程文件MediaType

using HttpRequestMessage request = new(HttpMethod.Get, remoteImagePath);
using var response = await httpClient.SendAsync(request);

if (response.Content.Headers.ContentType == null)
{
throw new Exception("无法获取Headers.ContentType信息");
}
var mediaType = response.Content.Headers.ContentType.MediaType;
if (mediaType == null)
{
throw new Exception("无法获取MediaType信息");
}
Console.WriteLine(mediaType.ToLower());
var imageExt = new[] { "image/gif", "image/jpeg", " image/png" };
if (!((IList)imageExt).Contains(mediaType.ToLower()))
{
throw new Exception("非图片文件");
}

posted @ 2021-11-17 12:03  冰封的心  阅读(137)  评论(0编辑  收藏  举报