0

I am trying to get the website's favicon's by adding "/favicon.ico" to the domain

I make a request, get the stream, convert it to a memorystream, and then to an image. For most sites, it works perfectly. For this one, I have a problem :"https://www.7sur7.be/"

Trying to get the favicon from https://www.7sur7.be/favicon.ico breaks when converting the memorystream to an image, because the stream is filled with the Cookies policy page.

Manually entering this address in a browser gets you to the cookies policy page, then to the icon What I need is a request recognized as accepting cookies.

Here's the shortest code I could write to expose the problem.

public void Test()
    {
        var url = @"https://www.7sur7.be/favicon.ico";
        //Try it with https://www.stackoverflow.com/favicon.ico and you'll end up with an Icon
        var memStream = new MemoryStream();
        Stream stream = GetStream(url);//defined below

        byte[] buffer = new byte[1024];
        int byteCount;
        do
        {
            byteCount = stream.Read(buffer, 0, buffer.Length);
            memStream.Write(buffer, 0, byteCount);
        } while (byteCount > 0);

        var img = Image.FromStream(memStream, true, true);
        var bm = new Bitmap(img);
        Icon ic = Icon.FromHandle(bm.GetHicon());
    }

Here is the original GetStream() method:

private static Stream GetStream(string url)
    {
        WebRequest requestImg = WebRequest.Create(url);
        WebResponse response = requestImg.GetResponse();
        return response.GetResponseStream();
    }

Inspired by this SO post, here is another GetStream() implementation:

public Stream GetStream(string url)
    {
        CookieContainer cookieContainer = new CookieContainer();
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.CookieContainer = cookieContainer;
        var response = (HttpWebResponse)httpWebRequest.GetResponse();

        HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest2.CookieContainer = cookieContainer;

        WebResponse response2 = httpWebRequest2.GetResponse();
        return response.GetResponseStream();
    }

Inspired by this SO post, here is another attempt:

(CookieAwareWebClient class in the link)

public void Test2()
    {
        var url = @"https://www.7sur7.be/favicon.ico";
        //Try it with https://www.stackoverflow.com/favicon.ico and you'll end up with an Icon
        var uri = new Uri(url);
        var host = uri.Host;
        CookieContainer cookieJar = new CookieContainer();
        var cook = new Cookie("mycookie", "cookievalue", "/", host);
        cookieJar.Add(cook);

        CookieAwareWebClient client = new CookieAwareWebClient(cookieJar);

        var bytes = client.DownloadData(url);
        Icon icon;
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            icon = new Icon(ms);
        }
    }

I tried other solutions found here and there, and nothing seems to work. Could someone make the above code work? The final solution should be portable to other computers. Thanks!

Eric P.
  • 97
  • 1
  • 7
  • After taking a quick look, it appears that unless you have their cookie on their computer their web server automatically provides output from the domain of myprivacy.persgroep.net and will not provide anything from the 7sur7.be site until that happens. – Brad Sep 28 '18 at 20:55
  • So, you think it cannot be reached programmatically? I tried to get the favicon with the google service (eg:https://www.google.com/s2/favicons?domain=7sur7.be). It works, but obviously, I'd like not to be dependant on them. Do you see a solution? Thanks! – Eric P. Sep 28 '18 at 21:07
  • The only way I can think of would be to programmatically accept the cookie agreement and have it then feed you the original request - in this case the favicon.ico file. – Brad Sep 29 '18 at 01:33
  • Ok, thank you for your time. In the context of the program I want to make, i could do something with browser automation. I'll go this way – Eric P. Sep 29 '18 at 07:35

0 Answers0