You must call the link page of a specified project in Windows service. Because the web browser or other browsers are used to access the page, the WebClient class is used.
If you only want to request a file from a specific URI, use WebClient, which is the simplest.. Net class. It only uses one or two commands to perform basic operations ,. net Framework currently supports Uris starting with HTTP:, https, and file: identifiers.
WebClient File Download
There are two methods for downloadfile using WebClient. The specific method depends on the processing method of the file content. If you only want to save the file to the disk, use the downloadfile () method, this method has two parameters: the requested URI and the data storage location of the request file.
More commonly, ApplicationsProgramThe openread method is used to process data retrieved from the web site. This method returns a stream object, which can be extracted from the data stream to the memory.
Example: openread (string URI );
Openread (string URI)
1 # Region Read the HTML of the specified URI 2 /// <Summary> 3 /// Read the HTML of the specified URI 4 /// </Summary> 5 /// <Param name = "sender"> </param> 6 /// <Param name = "E"> </param> 7 Private Void Button4_click ( Object Sender, eventargs E) 8 { 9 WebClient WC = New WebClient (); 10 String Uri = " Http: // 127.0.0.1/RSS/sina. aspx " ; 11 Stream stream = WC. openread (URI ); 12 Streamreader sr = New Streamreader (Stream ); 13 String Strline = "" ; 14 While (Strline = Sr. Readline ())! = Null ) 15 { 16 This . Listbox1.items. Add (strline ); 17 } 18 Sr. Close (); 19 } 20 # Endregion
Example: openwriter (string Uri, string method );
Openwriter (string Uri, string method)
1 # Region Open a stream and write data to the URI using the specified method. 2 /// <Summary> 3 /// Open a stream and write data to the URI using the specified method. 4 /// </Summary> 5 /// <Param name = "sender"> </param> 6 /// <Param name = "E"> </param> 7 Private Void Button#click (Object Sender, eventargs E) 8 { 9 WebClient WC = New WebClient (); 10 String Uri = " Http: // 192.168.0.35/cims30/rss.txt " ; 11 Stream stream = WC. openwrite (Uri, " Put " ); 12 Streamwriter Sw = New Streamwriter (Stream ); 13 Sw. writeline ( " Helloworldhelloworldhelloworldhelloworld " ); 14 Sw. Flush (); 15 Sw. Close (); 16 MessageBox. Show ( " OK " ); 17 } 18 # Endregion
The openwriter method returns a writable data stream, which allows you to send data to the URI. You can specify a method for sending data to the host. The default value is post, in the preceding example, assume that 0.35 of the servers have a writable directoryCodeIs to create the rss.txt file under the directory, whose content is "helloworldhelloworldhelloworld"
Upload files
The WebClient class provides the uploadfile () and uploaddata () methods, which can be used when you need to post an HTML form or upload the entire file. The uploadfile () method uploads the file to the specified location, where the file name is provided, and the uploaddata () method uploads the binary data provided by the byte array to the specified URI;
Example:
Upload files
1 # Region Upload a local file to a specified URI 2 /// <Summary> 3 /// Upload a local file to a specified URI 4 /// </Summary> 5 /// <Param name = "sender"> </param> 6 /// <Param name = "E"> </param> 7 Private Void Button2_click ( Object Sender, eventargs E) 8 { 9 WebClient WC = New WebClient (); 10 String Targetpath = " Http: // 127.0.0.1/RSS/Data configuration.zip " ; 11 String Sourcepath = " D: \ data configuration.zip " ; 12 This . Label1.text = String . Format ( " Uploading {0} to {1} " , Targetpath, sourcepath ); 13 Byte [] Bt = WC. uploadfile (targetpath, " Put " , Sourcepath ); 14 MessageBox. Show ( " OK " ); 15 } 16 # Endregion 17 18 19 # Region Uploads a data buffer to a specified resource. 20 /// <Summary> 21 /// Uploads a data buffer to a specified resource. 22 /// </Summary> 23 /// <Param name = "sender"> </param> 24 /// <Param name = "E"> </param> 25 Private Void Button3_click ( Object Sender, eventargs E) 26 { 27 WebClient WC = New WebClient (); 28 String Targetpath =" Http: // 127.0.0.1/RSS/kaifeng.jpg " ; 29 String Sourcepath = @" C: \ test.jpg " ; 30 Filestream FS = New Filestream (sourcepath, filemode. Open, fileaccess. Read ); 31 Byte [] Bt =New Byte [Fs. Length]; 32 FS. Read (BT, 0 , BT. Length ); 33 WC. uploaddata (targetpath, " Put " , BT ); 34 } 35 # Endregion
The WebClient has limited functions, especially the inability to use an authentication certificate. As a result, problems occur when uploading data. Currently, many websites do not accept files uploaded without authentication. Although you can add the title information to the request and check the corresponding title information, this is only a general purpose check. For any protocol, WebClient does not have specific support ,. This is because WebClient is a very general class and can use any protocol to send requests and accept corresponding content. It cannot process any features specific to any protocol.