存储来自Web服务的图像

Dav*_*ave 1 c# favicon httpwebrequest

我正在访问返回指定域的网站图标的API(http://getfavicon.appspot.com/).我有很长的域名列表,我想获取图标,并且不希望每次都调用Web服务,所以我想我会得到响应并将图像存储在文件系统或文件系统中DB Blob.

然而.我不知道如何从服务返回的响应流中获得有意义的东西.

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(); 
Run Code Online (Sandbox Code Playgroud)

我已经到了这里得到回复,但我怎么能把它当作可以保存到SQL DB或文件系统的东西呢?

我错过了一些简单的事吗?

谢谢

Dav*_*vid 5

如果您使用该System.Net.WebClient课程,您可以更轻松地完成此课程.

这将下载URL并将其保存到本地文件:

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");
Run Code Online (Sandbox Code Playgroud)

如果你想要一个byte [],请使用该DownloadData方法.