!!基础---c# 下载网页+图片
生活随笔
收集整理的這篇文章主要介紹了
!!基础---c# 下载网页+图片
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://www.cnblogs.com/sun8134/archive/2010/07/05/1771187.html
?
!!?WebClient 類使用 WebRequest 類提供對 Internet 資源的訪問
?
也是比較老的東西了
最近用到
記錄下以免以后忘了
要下載圖片首先要有圖片地址
要有圖片地址就要先把網(wǎng)頁下下來分析下URL
下載網(wǎng)頁一般用兩種方法
1,用?system.net.webclient
using System.Net; using System.Windows.Forms;string url = "http://www.cnblogs.com"; string result = null;try {WebClient client = new WebClient();result = client.DownloadString( url ); } catch (Exception ex) {MessageBox.Show( ex.Message ); }2,用?System.Net.HttpWebRequest
?
using System.Net; using System.IO; using System.Windows.Forms;string result = null; string url = "http://www.cnblogs.com"; WebResponse response = null; StreamReader reader = null;try {HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );request.Method = "GET";response = request.GetResponse();reader = new StreamReader( response.GetResponseStream(), Encoding.UTF8 );result = reader.ReadToEnd(); } catch (Exception ex) {MessageBox.Show( ex.Message ); } finally {if (reader != null)reader.Close();if (response != null)response.Close(); }?
?
至于怎么找圖片url略過,直接說下載圖片吧
其實(shí)和上面一樣,也有兩種方法:
1,WebRequest和WebResponse
WebRequest request = WebRequest.Create("http://images.cnblogs.com/logo_small.gif"); WebResponse response = request.GetResponse(); Stream reader = response.GetResponseStream(); FileStream writer = new FileStream("x:\\pic.jpg", FileMode.OpenOrCreate, FileAccess.Write); byte[] buff = new byte[512]; int c = 0; //實(shí)際讀取的字節(jié)數(shù) while ((c=reader.Read(buff, 0, buff.Length)) > 0) {writer.Write(buff, 0, c); } writer.Close(); writer.Dispose(); reader.Close(); reader.Dispose(); response.Close();?
2,WebClient
?
string url = "http://images.cnblogs.com/logo_small.gif"; string filepath = "x:\\pic.jpg"; WebClient mywebclient = new WebClient(); mywebclient.DownloadFile(url, filepath);轉(zhuǎn)載于:https://www.cnblogs.com/carl2380/archive/2011/11/23/2260593.html
總結(jié)
以上是生活随笔為你收集整理的!!基础---c# 下载网页+图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 超时集合_Java之集合(二十
- 下一篇: 利用JMF进行多媒体编程