1

I have some code that is using the solution from this question to validate that a certain image url exists.

So I have code that looks like this:

 private bool PictureExists(string id)
    {
        string url = "https://www.example.com/profile.do?userid=" + id;
        var request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "HEAD";

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

If a picture didn't exist this function would return false and it worked fine.

The issue now is that the function started returning true and I figured out the root cause is that they have changed the back end to return a default image if what I am searching for doesn't exist.

So my new requirement is how can I check if the image returned is not the default image (so I can replicate the validation that I had before)?

Community
  • 1
  • 1
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • 1
    Head usually returns size - it may be enough to check that (with some chance of false results if you can tolerate that). – Alexei Levenkov May 07 '15 at 18:55
  • what is the correct way to check head size? – leora May 07 '15 at 19:20
  • You could store a hash of the default image and compute the hash of the downloaded image, if they match then it is the default image. If there are multiple default images, you will need to store hashes of each and compare. – Michael Murphy May 07 '15 at 19:22
  • @leora Content-length header - http://stackoverflow.com/questions/3854842/content-length-header-with-head-requests . So if site always returns the same "missing-picture.png" it would have the same size which HEAD may return... – Alexei Levenkov May 07 '15 at 21:34

1 Answers1

1

Probably not the best solution, but my first idea was to convert the default image to byte array variable, and then download the image which you want to check, and convert it to byte array. Then you could compare them:

Code to compare (only from .NET 3.5!):

static public bool ByteArrayCompare(byte[] a1, byte[] a2) 
{
    return a1.SequenceEqual(a2);
}

Code to convert Image to byte[]:

static public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}

Hope it helps, and sorry for bad english.

RAPTOR
  • 124
  • 1
  • 11
  • Or probably the base64 string comparsion is more better, i don't know exactly, i'm just throwing ideas out of myself :) – RAPTOR May 07 '15 at 18:59
  • 1
    I'd combine this with the comment @Alexei Levenkov made to the question: first check the HEAD size, and then only download the image if you need to make a further check. – Joel Coehoorn May 07 '15 at 19:16
  • Absolutely smart suggetion, i'd also combine it. :) – RAPTOR May 07 '15 at 19:18
  • You could store a hash of the default image and compute the hash of the downloaded image, if they match then it is the default image. If there are multiple default images, you will need to store hashes of each and compare. – Michael Murphy May 07 '15 at 19:21
  • @MichaelMurphy - can you provide an example of how you would do that – leora May 07 '15 at 20:09
  • Look at this article on calculating an MD5 hash of files http://timtrott.co.uk/calculate-md5-checksum/ – Michael Murphy May 08 '15 at 07:38