亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#使用WebClient實(shí)現(xiàn)上傳下載

 更新時(shí)間:2022年05月13日 15:52:39   作者:springsnow  
這篇文章介紹了C#使用WebClient實(shí)現(xiàn)上傳下載的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、概述

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)的文件名,文件直接在瀏覽器上顯示或者在訪問(wè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);


/// 

/// 上傳過(guò)程處理
/// 
/// 
/// 
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(”//chabaoo.cn:2105/article/247999.htm?order=true”);

  • OriginalString 獲取傳遞給 Uri 構(gòu)造函數(shù)的原始 URI 字符串。//chabaoo.cn:2105/article/247999.htm?order=true
  • Scheme 獲取此 URI 的方案名稱。http
  • IsFile 獲取一個(gè)值,該值指示指定的 Uri 是否為文件 URI。false
  • Host 獲取此實(shí)例的主機(jī)部分。chabaoo.cn
  • 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 獲取用問(wèn)號(hào) (?) 分隔的 AbsolutePath 和 Query 屬性。/article/247999.htm?order=true

到此這篇關(guān)于C#使用WebClient實(shí)現(xiàn)上傳下載的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論