views:

38

answers:

1

I am accessing an API that returns a favicon for a specified domain (http://getfavicon.appspot.com/). I have a long list of domains that I want to get Icons for and don't want to make the call to the web service every time, so I figured I would get the response and store the image either on the file system or in a DB Blob.

However. I don't know how to get something meaningful from the response stream that comes back from the service.

byte[] buf = new byte[8192];

var request = (HttpWebRequest)WebRequest.Create("http://getfavicon.appspot.com/http://stackoverflow.com");

var response = (HttpWebResponse)request.GetResponse();

var resStream = response.GetResponseStream(); 

I've got as far as here to get a response back, but how would I can I treat this as something I can save to a SQL DB or out to the filesystem?

Am I missing something simple?

Thanks

+4  A: 

If you use the System.Net.WebClient class, you can do this a little easier.

This will download the URL and save it to a local file:

var client = new System.Net.WebClient();
client.DownloadFile(
    // Url to download
    @"http://getfavicon.appspot.com/http://stackoverflow.com",
    // Filename of where to save the downloaded content
    "stackoverflow.com.ico");

If you want a byte[] instead, use the DownloadData method.

David
Brilliant that's exactly what I needed to know. Great answer. Thanks.
Dave