您可以为此目的使用 HTTP HEAD 请求。通过这样做,您只会获得 HTTP 标头作为响应。这是一个简单的例子:
public bool ImageExists(string imageUri)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(imageUri);
request.Method = "HEAD";
try
{
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
return true;
else
return false;
}
catch
{
return false;
}
}
...这就是它的名称:
if (ImageExists(firstUri)
{
// Download from http://cdn.images.net...
}
else
{
// Download from http://www.example.com...
}
检查响应的 mime 类型是否为image/png也可能很有用,具体取决于您的请求在远程服务器上的处理方式。