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!