我想下载一些google的图片到本地,无奈google的技术太牛,搞了半天也不知道怎么用C#来实现
如原始图片地址:
如何用C#代码如何实现图片下载,并保存到本地?
使用 HttpWebRequest
然后你可以从
Content-Type: image/jpeg
Content-Disposition: inline;filename="unnamed.jpg"
判断图片类型.
1 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://lh4.googleusercontent.com/NFy-95h1MJmRW7ZoFhHa0oDzr6g6NiNQxQuLtkDkNoEhf_KkzhM0o9T3I6FHRdVpszbBBiRCmE8U9Cl4XXYiTFA87X3gMr77TUJemDzh3JDetOaDFvOEPt5g8a193IeC3qUl3-hZP9C0KO5YZCQD7ltrci9JDHrNR0wwBsz2ADk");
2 request.Method = "GET";
3
4 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
5 {
6 string fileName = response.Headers.Get("Content-Disposition");
7 fileName = fileName.Substring(fileName.IndexOf("\"")+1, fileName.LastIndexOf("\"") - fileName.IndexOf("\"")-1);
8
9 byte[] buf = new byte[response.ContentLength];
10 using (Stream stream = response.GetResponseStream())
11 {
12 stream.Read(buf, 0, buf.Length);
13 }
14
15 using (FileStream fs = File.Open(@"D:\" + fileName, FileMode.Create))
16 {
17 fs.Write(buf, 0, buf.Length);
18 fs.Flush();
19 }
20 }
直接用C#下载:http://lh4.googleusercontent.com/NFy-95h1MJmRW7ZoFhHa0oDzr6g6NiNQxQuLtkDkNoEhf_KkzhM0o9T3I6FHRdVpszbBBiRCmE8U9Cl4XXYiTFA87X3gMr77TUJemDzh3JDetOaDFvOEPt5g8a193IeC3qUl3-hZP9C0KO5YZCQD7ltrci9JDHrNR0wwBsz2ADk
就行了,这个链接输出的就是一张图片...就是绝对地址。