Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use WebClient to upload and download C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to upload and download C# using WebClient". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to upload and download C# using WebClient.

I. Overview

System.Net.WebClient belongs to the high-level class and is easy to use. The asynchronous version is supported. URI such as http,https,fpt,files is supported.

It is not recommended to use the WebClient class for new development. Please switch to System.Net.Http.HttpClient class for Net4.5 and above.

Download 1. OpenRead: open a readable Stream.

The RETR command is used by default for FTP resources, and the Get method is used by default for HTTP resources.

Stream= client.OpenRead (serverUri):

Give an example

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 opens a read-only stream StreamReader reader = new StreamReader (data, Encoding.GetEncoding ("gb2312")) for downloaded data Sting s = reader.ReadToEnd (); reader.Close (); return Sitt2, DownloadData: download resources in the form of byte []. Byte [] data= client.DownloadData (serverUri): 3. DownloadFile: download the resource to a local file. Void client.DownloadFile (serverUri,localFile): 4. DownloadString: download resources in the form of string. String content = client.DownloadString (serverUri): 5. Event

DownloadProgressChanged event:

Download***Completed event

6. Get the real file name of the download URL

Gets the content of the http header information.

Content-disposition is an extension of the MIME protocol, which instructs the MIME user agent how to display additional files.

Content-disposition can actually control the user's request to provide a default file name when the content is saved as a file, and the file is displayed directly on the browser or a file download dialog box pops up during access.

It is shaped like "Content-Disposition: attachment;filename=FileName.txt".

When you use this header message when the response type is application/octet- stream, it means that you don't want to display the content directly, but a "File download" dialog box pops up.

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; third, upload 1, OpenWrite: open a writable stream.

The STOR command is used by default for FTP resources, and the POST method is used by default for HTTP resources.

Strean = client.OpenWrite (serverUri); 2. UploadData: upload byte [] data to serverUri. Byte [] = client.UploadData (serverUri,byte []); 3. UploadFile: upload local files to serverUri. Byte [] = client.UploadFile (serverUri,localFile)

For example:

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); / upload process processing / 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 ("upload completed"); else throw e.Error;} 4, UploadValues: collects the specified name / value to serverUri. Byte [] = client.UploadValues (serverUri,namevalueCollection); 5. Event:

UploadProgressChanged:e.ProcessPercentage,e.TatalBytesToReceive,e.BytesReceived

Upload***Completed: e.Cancelled,e.Error,E.UserState

4. System.Url class (uniform resource identifier)

Uri url=new Uri ("/ / www.yisu.com:2105/article/247999.htm?order=true")

OriginalString gets the original URI string passed to the Uri constructor. / / www.yisu.com:2105/article/247999.htm?order=true

Scheme gets the scheme name for this URI. Http

IsFile gets a value indicating whether the specified Uri is a file URI. False

Host gets the host portion of this instance. Www.yisu.com

HostNameType gets the type of hostname specified in URI. DNS

Port gets the port number of this URI. 2105

IsDefaultPort gets a value indicating whether the port value of URI is the default value for this scenario. False

AbsolutePath gets the absolute path of the URI. / article/247999.htm

Query gets any query information included in the specified URI. ? order=true

PathAndQuery gets with a question mark (?) Separated AbsolutePath and Query properties. / article/247999.htm?order=true

At this point, I believe you have a deeper understanding of "how to upload and download C# using WebClient". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report