1

I am making a C# store app which contains pictures. I get the pictures from a website for example: http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG

I have 2 images, 1 is an image of the actual product. and 1 image is an image that there is no image available. Now i want to check if there is a picture behind the given URL, and if there is not, i want to load the image that there is no image available.

i got an object product which contains itemnumber, description and an imagepath. At this point i simply do this.

var url = "http://www.gravatar.com/avatar/6810d91caff032b202c50701dd3af745?d=identicon&r=PG";
Product p = new product (123, "productdescription", url);

if (url //if no results given){ p.url = imgpath2} // filepath with no image available picture

How can I make a simple check if the given url contains a picture, or will give me a "webpage not available"/ no content available error? thanks in advance.

edit: Note* i'm working with visual studio 2013, and I am building a C# store app.

Proliges
  • 371
  • 4
  • 26

2 Answers2

3

No need to download the entire image, just use HEAD:

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

bool exists;
try
{
    request.GetResponse();
    exists = true;
}
catch
{
   exists = false;
}

For more info, you can check this post for help with your issue:


[Update: If you want to make the call asynchronously...]

// 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;
    };
Community
  • 1
  • 1
Pierre
  • 417
  • 3
  • 9
  • I see how this can work. But the problem is I am making a C# store app. The only operation available for me is request.GetResponsAsync(); which requires me to make the method async. Long story short: When i enter an invalid webadres, it will provide me with the following error: A first chance exception of type 'System.Net.WebException' occurred in mscorlib.dll Additional information: The remote name could not be resolved – Proliges Aug 25 '14 at 14:13
  • You can make a sync call this way: WebResponse response = request.GetResponseAsync().Result; – Pierre Aug 25 '14 at 14:35
0

Try this:

    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);
    }

If the file exists it should return a true, if not it should return a false.

If you want to look at how to figure out how to find out if a url gives you any reponse, you can also take a look at this previous topic: C# How can I check if a URL exists/is valid?

Community
  • 1
  • 1
Adam
  • 93
  • 7