Add a getinputstream Extension Method to string.

Source: Internet
Author: User
Tags ftp protocol

Sometimes, we need to read some data, regardless of whether the data is from the data file on the disk or from the network. Therefore, the following stringextensions. CS is available:

 1   Using System;
2 Using System. IO;
3 Using System. net;
4
5 Namespace Skyiv
6 {
7 Public Static Class Stringextensions
8 {
9 Public Static Stream getinputstream ( This String Filenameoruri, String User = Null , String Password = Null )
10 {
11 If (! Uri. iswellformeduristring (filenameoruri, urikind. Absolute )) Return File. openread (filenameoruri );
12 VaR Uri = New Uri (filenameoruri );
13 If (URI. scheme = URI. urischemehttp | URI. scheme = URI. urischemehttps) Return Uri. gethttpstream ();
14 If (URI. scheme = URI. urischemeftp) Return Uri. getftpstream (user, password );
15 If (URI. scheme = URI. urischemefile) Return Uri. getfilestream ();
16 Throw New Notsupportedexception ( " Notsupported URI scheme: " + URI. scheme );
17 }
18
19 Static Stream getftpstream ( This Uri, String User = Null , String Password = Null )
20 {
21 VaR FTP = (ftpwebrequest) webrequest. Create (URI );
22 If (User! = Null & User! = " Anonymous " & User! = " FTP " )
23 FTP. Credentials = New Networkcredential (user, password );
24 FTP. method = webrequestmethods. FTP. downloadfile;
25 Return (Ftpwebresponse) FTP. getresponse (). getresponsestream ();
26 }
27
28 Static Stream gethttpstream ( This Uri URI)
29 {
30 Return (Httpwebresponse) (httpwebrequest) webrequest. Create (URI). getresponse (). getresponsestream ();
31 }
32
33 Static Stream getfilestream ( This Uri URI)
34 {
35 Return (Filewebresponse) (filewebrequest) webrequest. Create (URI). getresponse (). getresponsestream ();
36 }
37 }
38 }

AboveProgramMedium:

    1. The getinputstream Extension Method of rows 9th to 17 returns the input stream of the data to be read.
    2. Line 3 calls the static method iswellformeduristring Of The URI class to determine whether to read data from the network. If not, call the static method openread of the file class to obtain the input stream.
    3. Line 3 constructs an instance of the URI class.
    4. Line 3 processes the input using HTTP or HTTPS.
    5. Row 3 processes the input using the FTP protocol.
    6. Row 3 processes the input using the file protocol.
    7. To handle other protocols in line 16th, A notsupportedexception exception is thrown directly, indicating that only the above four protocols are supported.
    8. The getftpstream Extension Method of rows 19th to 26 is used to obtain the input stream of the response data sent on the FTP server. It can be anonymous or non-Anonymous.
    9. The gethttpstream Extension Method of lines 28th to 31 is used to obtain network streams using HTTP or HTTPS.
    10. The getfilestream Extension Method of lines 33rd to 36 is used to obtain the data stream of the local disk file system using the file protocol.

The copytester. CS for testing is as follows:

 1   Using System. IO;
2
3 Namespace Skyiv. Test
4 {
5 Static Class Copytester
6 {
7 Static Void Main ( String [] ARGs)
8 {
9 ARGs [ 0 ]. Getinputstream (). copyto (file. Create (ARGs [ 1 ]);
10 }
11 }
12 }

The function of this test program is to copy data. Two command line parameters are required:

    1. The first command line parameter specifies the data source, which can be a local disk file or network data. It supports https, HTTP, FTP, and file protocols. Of course, the file protocol actually reads local disk files.
    2. The second command line parameter specifies the name of the local disk file to be copied.

The content of the above test procedure is the statement of Line 1:

    1. Use the first command line parameter ARGs [0] To Call The getinputstream Extension Method of the string class to obtain the input stream.
    2. Call the copyto method of the stream class to copy the input stream to the output stream.
    3. The output stream is obtained by using the static file method create.

Compile and run in Windows:

E: \ work> CSC copytester. CS stringextensions. CS Microsoft (r) Visual C #2010 compiler 4.0.30319.1 copyright (c) Microsoft Corporation. All rights reserved. E: \ work> Copytester https://github.com/mono/xsp/zipball/master mono-xsp.zip E: \ work> Copytester http://mysql.ntu.edu.tw/Downloads/Connector-Net/mysql-connector-net-6.5.4-noinstall.zip mysql-connector.zip E: \ work> Copytester ftp://ftp.ntu.edu.tw/pub/MySQL/Downloads/Connector-Net/mysql-connector-net-6.5.4-noinstall.zip mysql-connector.2.zip E: \ work> Copytester file: /// E:/work/mysql-connector.zip mysql-connector.3.zip E: \ work> Copytester mysql-connector.zip mysql-connector.4.zip E: \ work> Dir *. Zip Mono-xsp.zip2012/468,024 mysql-connector.2.zip2012/03/11 mysql-connector.3.zip2012/4,176,361 mysql-connector.4.zip2012/03/11 4,176,361 mysql-connector.zip/03/11 4,176,361

The above tests respectively read network data using https, HTTP, FTP, and file protocols, and read data from local disks. Note that the file protocol actually reads data from the local disk.

Compile and run in Linux:

 Ben @ vbox :~ /Work>  DMCS copytester. CS stringextensions. CS  Ben @ vbox :~ /Work>  mono copytester.exe http://mysql.ntu.edu.tw/Downloads/Connector-Net/mysql-connector-net-6.5.4-noinstall.zip mysql-connector.zip  Ben @ vbox :~ /Work>  mono copytester.exe ftp://ftp.ntu.edu.tw/pub/MySQL/Downloads/Connector-Net/mysql-connector-net-6.5.4-noinstall.zip mysql-connector.2.zip  Ben @ vbox :~ /Work>  mono copytester.exe file: // home/BEN/work/mysql-connector.zip mysql-connector.3.zip  Ben @ vbox :~ /Work>  mono copytester.exe mysql-connector.zip mysql-connector.4.zip  Ben @ vbox :~ /Work>  ls-L *. zip -RW-r -- 1 Ben users 4176361 Mar 11 mysql-connector.2.zip-rw-r -- r -- 1 Ben users 4176361 Mar 11 mysql-connector.3.zip-rw-r -- r -- 1 Ben users 4176361 Mar 11 mysql-connector.4.zip-rw-r -- r -- 1 Ben users 4176361 Mar 11 mysql-connector.zip 

In Windows, the HTTPS data stream on the network can be normally read, but fails in Linux:

Ben @ vbox :~ /Work> Mono copytester.exe https://github.com/mono/xsp/zipball/master mono-xsp.zip Unhandled exception: system. net. webexception: Error getting response stream (write: The authentication or decryption has failed .): sendfailure ---> system. io. ioexception: the authentication or decryption has failed. ---> mono. security. protocol. TLS. tlsexception: Invalid certificate received ed from server. error code: 0xffffffff800b010a at Mono. security. protocol. TLS. handshake. client. tlsservercertificate. validatecertificates (mono. security. x509.x509certificatecollection certificates) [0x00000] In: 0 at Mono. security. protocol. TLS. handshake. client. tlsservercertificate. processastls1 () [0x00000] In: 0 at Mono. security. protocol. TLS. handshake. handshakemessage. process () [0x00000] In: 0 at (wrapper remoting-invoke-with-check) Mono. security. protocol. TLS. handshake. handshakemessage: Process () at Mono. security. protocol. TLS. clientrecordprotocol. processhandshakemessage (mono. security. protocol. TLS. tlsstream handmsg) [0x00000] In: 0 at Mono. security. protocol. TLS. recordprotocol. internalreceiverecordcallback (iasyncresult asyncresult) [0x00000] In: 0 --- end of inner exception stack trace --- at Mono. security. protocol. TLS. sslstreambase. asynchandshakecallback (iasyncresult asyncresult) [0x00000] In: 0 --- end of inner exception stack trace --- at system. net. httpwebrequest. endgetresponse (iasyncresult asyncresult) [0x00000] In: 0 at system. net. httpwebrequest. getresponse () [0x00000] In: 0 at skyiv. stringextensions. gethttpstream (system. uri URI) [0x00000] In: 0 at skyiv. stringextensions. getinputstream (system. string filenameoruri, system. string user, system. string password) [0x00000] In: 0 at skyiv. test. copytester. main (system. string [] ARGs) [0x00000] In: 0 [Error] fatal unhandled exception: system. net. webexception: Error getting response stream (write: The authentication or decryption has failed .): sendfailure ---> system. io. ioexception: the authentication or decryption has failed. ---> mono. security. protocol. TLS. tlsexception: Invalid certificate received ed from server. error code: 0xffffffff800b010a at Mono. security. protocol. TLS. handshake. client. tlsservercertificate. validatecertificates (mono. security. x509.x509certificatecollection certificates) [0x00000] In: 0 at Mono. security. protocol. TLS. handshake. client. tlsservercertificate. processastls1 () [0x00000] In: 0 at Mono. security. protocol. TLS. handshake. handshakemessage. process () [0x00000] In: 0 at (wrapper remoting-invoke-with-check) Mono. security. protocol. TLS. handshake. handshakemessage: Process () at Mono. security. protocol. TLS. clientrecordprotocol. processhandshakemessage (mono. security. protocol. TLS. tlsstream handmsg) [0x00000] In: 0 at Mono. security. protocol. TLS. recordprotocol. internalreceiverecordcallback (iasyncresult asyncresult) [0x00000] In: 0 --- end of inner exception stack trace --- at Mono. security. protocol. TLS. sslstreambase. asynchandshakecallback (iasyncresult asyncresult) [0x00000] In: 0 --- end of inner exception stack trace --- at system. net. httpwebrequest. endgetresponse (iasyncresult asyncresult) [0x00000] In: 0 at system. net. httpwebrequest. getresponse () [0x00000] In: 0 at skyiv. stringextensions. gethttpstream (system. uri URI) [0x00000] In: 0 at skyiv. stringextensions. getinputstream (system. string filenameoruri, system. string user, system. string password) [0x00000] In: 0 at skyiv. test. copytester. main (system. string [] ARGs) [0x00000] In: 0

I don't know if it is my opensuse 12.1 operating system or the mono 2.10.6 runtime environment. Some configuration is required to meet the HTTPS security verification requirements. However, I have not made any special configuration in windows. In addition, you can use the wget command in Linux to download HTTPS data:

Ben @ vbox :~ /Work> Wget https://github.com/mono/xsp/zipball/master Asking libproxy about URL 'https: // github.com/mono/xsp/zipball/master'libproxy suggest to use 'direct: // '-- 13:48:32 -- https://github.com/mono/xsp/zipball/masterResolving github.com (github.com )... 207.97.227.239connecting to github.com (github.com) | 207.97.227.239 |: 443... connected. HTTP request sent, awaiting response... 302 foundlocation: https://nodeload.github.com/mono/xsp/zipball/mast Er [following] asking libproxy about URL 'https: // your suggest to use 'direct: // '-- 2012-03-11 13:48:34 -- https://nodeload.github.com/mono/xsp/zipball/masterResolving nodeload.github.com (nodeload.github.com )... 207.97.227.252connecting to nodeload.github.com (nodeload.github.com) | 207.97.227.252 |: 443... connected. HTTP request sent, awaiting response... 200 oklength: 468024 (457 K) [application/octet-stream] saving: 'master' 100% [========================================== ==>] 468,024 46.5 K/s in 17s2012-03-11 13:48:54 (27.0 kb/s) -'master' saved [468024/468024] Ben @ vbox :~ /Work>Mv master mono-xsp.zip 

In this case, the httpwebrequest class in the mono environment may need some settings to read HTTPS data. If any of you know, please let me know in the comments in this article. Thank you.

References
    1. Msdn: URI class (system)
    2. Msdn: File class (system. Io)
    3. Msdn: webrequest class (system. Net)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.