C#商店应用程序检查URL中的图片是否可用

C#商店应用程序检查URL中的图片是否可用,c#,url,windows-store-apps,C#,Url,Windows Store Apps,我正在制作一个包含图片的C#store应用程序。我从网站上获取图片,例如: 我有两张图片,一张是实际产品的图片。并且1图像是没有可用图像的图像。现在我想检查给定URL后面是否有图片,如果没有,我想加载没有可用图片的图片 我得到了一个包含itemnumber、description和imagepath的对象产品。在这一点上,我只是这样做 var url = "http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=iden

我正在制作一个包含图片的C#store应用程序。我从网站上获取图片,例如:

我有两张图片,一张是实际产品的图片。并且1图像是没有可用图像的图像。现在我想检查给定URL后面是否有图片,如果没有,我想加载没有可用图片的图片

我得到了一个包含itemnumber、description和imagepath的对象产品。在这一点上,我只是这样做

var url = "http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG";
Product p = new product (123, "productdescription", url);
if(url//if未给出结果){p.url=imgpath2}//filepath,无图像可用图片

如何简单检查给定url是否包含图片,或者是否会给我一个“网页不可用”/“无内容可用”错误?提前谢谢

编辑:注意*我正在使用visual studio 2013,我正在构建一个C#store应用程序

试试这个:

    var url = "http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG";
    if(File.Exists(url)){
         Product p = new product (123, "productdescription", url);
    }
    else{
         Product p = new product (123, "productdescription", imgpath2);
    }
如果文件存在,则应返回true,否则应返回false

如果您想了解如何找出url是否提供响应,您还可以查看前面的主题:
无需下载整个图像,只需使用HEAD:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
request.Method = "HEAD";

bool exists;
try
{
    request.GetResponse();
    exists = true;
}
catch
{
   exists = false;
}
有关详细信息,您可以查看有关问题的帮助:


[更新:如果要异步调用…]

// Initialize your product with the 'blank' image
Product p = new Product(123, "productdescription", imgpath2);

// Initialize the request
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
request.Method = "HEAD";

// Get the response async
Task<WebResponse> response = request.GetResponseAsync();

// Get the response async
response.AsAsyncAction().Completed += (a, b) =>
    {
        // Assign the proper image, if exists, when the task is completed
        p.URL = url;
    };
//使用“空白”图像初始化产品
产品p=新产品(123,“产品描述”,imgpath2);
//初始化请求
HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(“url”);
request.Method=“HEAD”;
//异步获取响应
任务响应=request.GetResponseAsync();
//异步获取响应
response.asasAsyncAction().Completed+=(a,b)=>
{
//当任务完成时,分配适当的映像(如果存在)
p、 URL=URL;
};

产品构造函数是什么样子的?我知道这是如何工作的。但问题是我正在制作一个C#store应用程序。我唯一可用的操作是request.getResponseAsync();这需要我使方法异步。长话短说:当我输入一个无效的WebAddress时,它将向我提供以下错误:mscorlib.dll中发生了类型为“System.Net.WebException”的第一次意外异常附加信息:无法解析远程名称您可以通过以下方式进行同步调用:WebResponse response=request.GetResponseAsync().Result;