0
public void BuildImg()
{
    // The two different images as strings.
    string url1 = "http://remoteimage.com/image.jpg";
    string url2 = "http://remoteimage.com/image2.jpg";

    try
    {
        // Check to see if url1 exists or not
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
        request.Credentials = System.Net.CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        myImg.Visible = true;
        myImg.ImageUrl = url1;
    }
    catch (Exception ex)
    {
        // Check to see if url2exists or not
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url2);
        request.Credentials = System.Net.CredentialCache.DefaultCredentials;
        HttpWebResponse response;
        try
        {
            response = request.GetResponse() as HttpWebResponse;
        }
        catch (WebException exc)
        {
            response = exc.Response as HttpWebResponse;
        }
        // Set myImg to show if url2 exists
        myImg.Visible = true;
        myImg.ImageUrl = url2;

        // If response returns 404, then hide myImg
        if (response.StatusCode == HttpStatusCode.NotFound)
        {
            myImg.Visible = false;
        }

    }
Oded
  • 489,969
  • 99
  • 883
  • 1,009
Trey
  • 1
  • 1
  • 1
  • When you're sure in resulting type (and do null-check), use `case`, not operator `as`, i.e. `(HttpWebResponse)request.GetResponse()` – abatishchev Jan 21 '11 at 15:35
  • 1
    Is this a question or an answer? – Oded Jan 21 '11 at 15:40
  • 1
    only putting code is not enough to answer. You have to Explain what you want, to get better answers.. – Javed Akram Jan 21 '11 at 15:43
  • Possible duplicate: http://stackoverflow.com/questions/1639878/how-can-i-check-if-an-image-exists-at-http-someurl-myimage-jpg-in-c-asp-net – marquito Aug 23 '13 at 15:39

1 Answers1

1
var arr = new[]
{
    "http://example.com/image.jpg",
    "http://example.com/image2.jpg"
    ...
};

myImg.ImageUrl = arr.FirstOrDefault(i => CheckExistence(i));

static bool CheckUrlExistence(string url)
{
    try
    {
        var request = (HttpWebRequest)WebRequest.Create(url);
        request.Credentials = CredentialCache.DefaultCredentials;
        request.Method = "HEAD";
        var response = (HttpWebResponse)request.GetResponse();
        return response.StatusCode == HttpStatusCode.OK;
    }
    catch (Exception ex)
    {
        var code = ((HttpWebResponse)((WebException)ex).Response).StatusCode; // NotFound, etc
        return false;
    }
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1. A non-existant page does not result in an exception but rather a response with a 404 error code. 2. Even if it were to throw, catching every exception an assuming they all mean that the page does not exist is pretty bad form. – Paul Ruane Jan 21 '11 at 15:46
  • @Paul: Agree. I just rewrote OP's code. See my edit - what do you think? – abatishchev Jan 21 '11 at 15:49
  • 3
    The best way I can think of to test the existance of a web resource would be to make a HEAD request. That way one can confirm the existance of the resource without having to pay the price to download it. You could make a HEAD request by setting the `WebRequest.Method` to "HEAD" — a response of 404 would indicate that it doesn't exist. (One should carefully handle the case where the 'HEAD' method is not supported by the web-server — a fallback to 'GET' perhaps.) – Paul Ruane Jan 21 '11 at 15:52
  • Definitely what @Paul Ruane said, use `HEAD`, that's the whole point of it – Chris Haas Jan 21 '11 at 15:54