網(wǎng)站首頁 編程語言 正文
一、概述
System.Net.WebClient屬于高層類、使用簡(jiǎn)單。均支持異步版本。支持http,https,fpt,files等URI。
建議不要將?
WebClient
?類用于新的開發(fā)。Net4.5及以上請(qǐng)改用?System.Net.Http.HttpClient?類。
二、下載
1、OpenRead:打開一個(gè)可讀的Stream。
對(duì)于FTP資源,默認(rèn)使用RETR命令;對(duì)于HTTP資源,默認(rèn)使用Get方法。
Stream= client.OpenRead(serverUri):
舉例
WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); client.Encoding = Encoding.GetEncoding("gb2312"); client.Credentials = new NetworkCredential("csp", "welcome"); Stream data = client.OpenRead(url);//OpenRead為下載的數(shù)據(jù)打開一個(gè)只讀的流 StreamReader reader = new StreamReader(data, Encoding.GetEncoding("gb2312")); sting s = reader.ReadToEnd(); reader.Close(); return s;
2、DownloadData:以byte[]形式下載資源。
byte[] data= client.DownloadData(serverUri):
3、DownloadFile:將資源下載在本地文件。
void client.DownloadFile(serverUri,localFile):
4、DownloadString:以string的形式下載資源。
string content =client.DownloadString(serverUri):
5、事件
- DownloadProgressChanged事件:
- Download***Completed事件
6、獲取下載網(wǎng)址的真實(shí)文件名
獲取http頭部信息的內(nèi)容。
Content-disposition 是 MIME 協(xié)議的擴(kuò)展,MIME 協(xié)議指示 MIME 用戶代理如何顯示附加的文件。
Content-disposition其實(shí)可以控制用戶請(qǐng)求所得的內(nèi)容存為一個(gè)文件的時(shí)候提供一個(gè)默認(rèn)的文件名,文件直接在瀏覽器上顯示或者在訪問時(shí)彈出文件下載對(duì)話框。?
形如:”Content-Disposition: attachment;filename=FileName.txt“。
當(dāng)你在響應(yīng)類型為application/octet- stream情況下使用了這個(gè)頭信息的話,那就意味著你不想直接顯示內(nèi)容,而是彈出一個(gè)"文件下載"的對(duì)話框
WebClient client = new WebClient(); byte[] data = client.DownloadData(fileUrl); var mc = Regex.Matches(Server.UrlDecode(client.ResponseHeaders["Content-Disposition"]), @"filename=(.+)"); string filename = mc[0].Groups[1].Value;
三、上傳
1、OpenWrite:打開一個(gè)可寫的流。
對(duì)于FTP資源,默認(rèn)使用STOR命令;對(duì)于HTTP資源,默認(rèn)使用POST方法。
Strean =client.OpenWrite(serverUri);
2、UploadData:將byte[]數(shù)據(jù)上傳到serverUri。
byte[] =client.UploadData(serverUri,byte[]);
3、UploadFile:將本地文件上傳到serverUri。
byte[] =client.UploadFile(serverUri,localFile);
舉例:
WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; client.Credentials = new NetworkCredential("csp", "welcome"); client.UploadProgressChanged += new UploadProgressChangedEventHandler(UploadProgressCallback); client.UploadFileCompleted += new UploadFileCompletedEventHandler(UploadFileCompletedCallback); client.UploadFileAsync(new Uri(uriString + Path.GetFileName(localfileName)), null, localfileName, progressbarfrom); /// /// 上傳過程處理 /// /// /// private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e) { (e.UserState as ProgressBar).Value = e.ProgressPercentage; } private static void UploadFileCompletedCallback(object sender, UploadFileCompletedEventArgs e) { if (e.Error == null) MessageBox.Show("完成上傳"); else throw e.Error; }
4、UploadValues:將指定的名/值集合到serverUri。
byte[] =client.UploadValues(serverUri,namevalueCollection);
5、事件:
- UploadProgressChanged:e.ProcessPercentage,e.TatalBytesToReceive,e.BytesReceived;
- Upload***Completed: e.Cancelled,e.Error,E.UserState;
四、System.Url類(統(tǒng)一資源表示符)
Uri url=new Uri(”//www.jb51.net:2105/article/247999.htm?order=true”);
- OriginalString 獲取傳遞給 Uri 構(gòu)造函數(shù)的原始 URI 字符串。//www.jb51.net:2105/article/247999.htm?order=true
- Scheme 獲取此 URI 的方案名稱。http
- IsFile 獲取一個(gè)值,該值指示指定的 Uri 是否為文件 URI。false
- Host 獲取此實(shí)例的主機(jī)部分。www.jb51.net
- HostNameType 獲取 URI 中指定的主機(jī)名的類型。DNS
- Port 獲取此 URI 的端口號(hào)。2105
- IsDefaultPort 獲取一個(gè)值,該值指示 URI 的端口值是否為此方案的默認(rèn)值。false
- AbsolutePath 獲取 URI 的絕對(duì)路徑。/article/247999.htm
- Query 獲取指定 URI 中包括的任何查詢信息。?order=true
- PathAndQuery 獲取用問號(hào) (?) 分隔的 AbsolutePath 和 Query 屬性。/article/247999.htm?order=true
原文鏈接:https://www.cnblogs.com/springsnow/p/9433995.html
相關(guān)推薦
- 2022-08-05 C語言超詳細(xì)講解宏與指針的使用_C 語言
- 2022-05-15 C++中類的轉(zhuǎn)換函數(shù)你了解嗎_C 語言
- 2022-12-01 Python?subprocess庫六個(gè)實(shí)例快速掌握_python
- 2022-11-08 詳解Python中數(shù)據(jù)處理的方法總結(jié)及實(shí)現(xiàn)_python
- 2023-06-17 python?__init__與?__new__的區(qū)別_python
- 2022-07-09 鼠標(biāo)事件-事件對(duì)象
- 2022-06-30 Oracle在PL/SQL中使用子查詢_oracle
- 2022-07-13 Docker之Harbor私有倉庫
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支