有时候,我们需要读取一些数据,而不管这数据来源于磁盘上的数据文件,还是来源于网络上的数据。于是,就有了下面的 StringExtensions.cs:
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 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 }
上述程序中:
下面就是测试用的 CopyTester.cs:
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 }
这个测试程序的功能就是拷贝数据,需要两个命令行参数:
上述测试程序实质内容就是第 9 行的语句:
在 Windows 操作系统中编译和运行:
E:\work> csc CopyTester.cs StringExtensions.cs Microsoft(R) Visual C# 2010 编译器 4.0.30319.1 版 版权所有(C) Microsoft Corporation。保留所有权利。 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 2012/03/11 09:35 468,024 mono-xsp.zip 2012/03/11 09:37 4,176,361 mysql-connector.2.zip 2012/03/11 09:38 4,176,361 mysql-connector.3.zip 2012/03/11 09:38 4,176,361 mysql-connector.4.zip 2012/03/11 09:36 4,176,361 mysql-connector.zip
上面分别测试了以 https、http、ftp、file 协议读取网络数据,以及从本地磁盘上读取数据。注意,file 协议实际上还是从本地磁盘读取数据。
在 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--r-- 1 ben users 4176361 Mar 11 09:54 mysql-connector.2.zip -rw-r--r-- 1 ben users 4176361 Mar 11 10:01 mysql-connector.3.zip -rw-r--r-- 1 ben users 4176361 Mar 11 10:01 mysql-connector.4.zip -rw-r--r-- 1 ben users 4176361 Mar 11 09:53 mysql-connector.zip
在 Windows 操作系统可以正常读取网络上的 https 数据流,在 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 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 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
不知道是不是我的 openSUSE 12.1 操作系统或者是 Mono 2.10.6 运行环境还需要进行一些配置,以便满足 https 的安全验证要求。但是我在 Windows 操作系统中也没有进行特别的配置。而且在 Linux 操作系统中使用 wget 命令也能够下载 https 协议的数据:
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://' --2012-03-11 13:48:32-- https://github.com/mono/xsp/zipball/master Resolving github.com (github.com)... 207.97.227.239 Connecting to github.com (github.com)|207.97.227.239|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://nodeload.github.com/mono/xsp/zipball/master [following] asking libproxy about url 'https://nodeload.github.com/mono/xsp/zipball/master' libproxy suggest to use 'direct://' --2012-03-11 13:48:34-- https://nodeload.github.com/mono/xsp/zipball/master Resolving nodeload.github.com (nodeload.github.com)... 207.97.227.252 Connecting to nodeload.github.com (nodeload.github.com)|207.97.227.252|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 468024 (457K) [application/octet-stream] Saving to: `master' 100%[======================================>] 468,024 46.5K/s in 17s 2012-03-11 13:48:54 (27.0 KB/s) - `master' saved [468024/468024] ben@vbox:~/work> mv master mono-xsp.zip
这样看来,Mono 环境的 HttpWebRequest 类可能需要进行一些设置才能读取 https 协议的数据。如果有哪位园友知道的话,请在本文的评论里告诉我,谢谢。