WebClient WebClient Usage Summary Usage Summary

Come to realize the project can call the specified project links page in the windows service. Due to access the page when using ie browser or other browsers, so think of using webclient class.

If you want to request a file from a specific URI, using WebClient, it is the simplest .NET class, which only perform basic operations by a two commands, .NET FRAMEWORK currently supporting at http:, https and file: Identifier beginning the uri.

WebClient to download files

Use webclient There are two ways to download files, which method to use depends on the contents of the file handling, if you only want to save the file to disk, use downloadfile () method, this method has two parameters, namely the request uri and the request file data storage location.

More commonly, the application needs to process data retrieved from the web site to use OpenRead for this method, this method returns a Stream object, then, the Stream object may be extracted from the data stream into memory.

Example: OpenRead (string uri);

OpenRead(string uri)
 #region 读取指定uri的html
        /// <summary>
        /// 读取指定uri的html
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            WebClient wc = new WebClient();
            string uri = "http://127.0.0.1/rss/sina.aspx";
            Stream stream = wc.OpenRead(uri);
            StreamReader sr = new StreamReader(stream);
            string strLine = "";
            while ((strLine = sr.ReadLine()) != null)
            {
                this.listBox1.Items.Add(strLine);
            }
            sr.Close();
        }
 #endregion

示例:OpenWriter(string uri,string method);

OpenWriter ( String URI, String Method)
 #region open a stream using the specified method of writing data to the URI
         ///  <Summary> 
        /// open a stream using the specified method of writing data to the URI
         ///  </ Summary> 
        ///  <param name = "SENDER"> </ param> 
        ///  <param name = "E"> </ param> 
        Private  void the button1_Click ( Object SENDER, EventArgs E)
        {
            WebClient wc = new WebClient();
            string uri = "http://192.168.0.35/cims30/rss.txt";
            Stream stream = wc.OpenWrite(uri, "PUT");
            StreamWriter sw = new StreamWriter(stream);
            sw.WriteLine("HelloWorldHelloWorldHelloWorldHelloWorld");
            sw.Flush();
            sw.Close();
            MessageBox.Show("OK");
        }
#endregion

openwriter method returns a writable data stream, user data sent to the URI, to specify the user to send data to the host machine used, the default is POST, the embodiment assumes a writable directory Ci Ma s on 0.35 server, this code is created rss.txt file in the directory, which reads "HelloWorldHelloWorldHelloWorldHelloWorld"

upload files

WebClient class provides UploadFile () and UploadData () method, the need to deliver an HTML form or upload the entire file when you can use these two methods. UploadFile () method to upload the file to the specified location, where the file name has been given, UploadData () method to upload binary data supplied to the byte arrays specified URI;

Example:

Upload
   #region upload files to the specified local URI
         ///  <Summary> 
        /// upload files to the specified local URI
         ///  </ Summary> 
        ///  <param name = "SENDER"> </ param> 
        / //  <param name = "E"> </ param> 
        Private  void the button2_Click ( Object SENDER, EventArgs E)
        {
            WebClient wc = new WebClient();
            string targetPath = "http://127.0.0.1/rss/Data Configuration.zip";
            string sourcePath = "d:\\Data Configuration.zip";
            this.label1.Text = string.Format("uploading {0} to {1}", targetPath, sourcePath);
            byte[] bt = wc.UploadFile(targetPath, "PUT", sourcePath);
            MessageBox.Show("OK");
        }
        #endregion


        #region the upload data buffer specified resource
         ///  <Summary> 
        /// the specified resource data buffer upload
         ///  </ Summary> 
        ///  <param name = "SENDER"> </ param> 
        / //  <param name = "E"> </ param> 
        Private  void the button3_Click ( Object SENDER, EventArgs E)
        {
            WebClient wc = new WebClient();
            string targetPath = "http://127.0.0.1/rss/kaifeng.jpg";
            string sourcePath = @"C:\test.jpg";
            FileStream fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
            byte[] bt = new byte[fs.Length];
            fs.Read(bt, 0, bt.Length);
            wc.UploadData(targetPath, "PUT", bt);
        }
        #endregion

webclient limited functionality, in particular, can not use an authentication certificate, so that problems arise when uploading data, now not many sites will accept uploaded files without authentication. Although you can add a title to request information and check the corresponding information in the title, but only in a general sense to check for any agreement, webclient no specific support. This is because webclient is very general class can use any appropriate protocol to send and receive requests, it can not handle any of the characteristics of any specific protocol.

Original link: WebClient Usage Summary

Guess you like

Origin www.cnblogs.com/rainbow70626/p/12099671.html