全部博文(372)
2012年(372)
分类: 虚拟化
2012-03-11 16:51:34
有时候,我们需要读取一些数据,而不管这数据来源于磁盘上的数据文件,还是来源于网络上的数据。于是,就有了下面的 StringExtensions.cs:
上述程序中:
下面就是测试用的 CopyTester.cs:
这个测试程序的功能就是拷贝数据,需要两个命令行参数:
上述测试程序实质内容就是第 9 行的语句:
在 Windows 操作系统中编译和运行:
E:\work> csc CopyTester.cs StringExtensions.csMicrosoft(R) Visual C# 2010 编译器 4.0.30319.1 版版权所有(C) Microsoft Corporation。保留所有权利。E:\work> CopyTester mono-xsp.zipE:\work> CopyTester mysql-connector.zipE:\work> CopyTester ftp://ftp.ntu.edu.tw/pub/MySQL/Downloads/Connector-Net/mysql-connector-net-6.5.4-noinstall.zip mysql-connector.2.zipE:\work> CopyTester file:///E:/work/mysql-connector.zip mysql-connector.3.zipE:\work> CopyTester mysql-connector.zip mysql-connector.4.zipE:\work> dir *.zip2012/03/11 09:35 468,024 mono-xsp.zip2012/03/11 09:37 4,176,361 mysql-connector.2.zip2012/03/11 09:38 4,176,361 mysql-connector.3.zip2012/03/11 09:38 4,176,361 mysql-connector.4.zip2012/03/11 09:36 4,176,361 mysql-connector.zip上面分别测试了以 https、http、ftp、file 协议读取网络数据,以及从本地磁盘上读取数据。注意,file 协议实际上还是从本地磁盘读取数据。
在 Linux 操作系统中编译和运行:
ben@vbox:~/work> dmcs CopyTester.cs StringExtensions.csben@vbox:~/work> mono CopyTester.exe mysql-connector.zipben@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.zipben@vbox:~/work> mono CopyTester.exe file:///home/ben/work/mysql-connector.zip mysql-connector.3.zipben@vbox:~/work> mono CopyTester.exe mysql-connector.zip mysql-connector.4.zipben@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 mono-xsp.zipUnhandled 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.Tl***ception: 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.Tl***ception: 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 asking libproxy about url ''libproxy suggest to use 'direct://'--2012-03-11 13:48:32-- Resolving 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: [following]asking libproxy about url ''libproxy suggest to use 'direct://'--2012-03-11 13:48:34-- Resolving 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 (457K) [application/octet-stream]Saving to: `master'100%[======================================>] 468,024 46.5K/s in 17s2012-03-11 13:48:54 (27.0 KB/s) - `master' saved [468024/468024]ben@vbox:~/work> mv master mono-xsp.zip这样看来,Mono 环境的 HttpWebRequest 类可能需要进行一些设置才能读取 https 协议的数据。如果有哪位园友知道的话,请在本文的评论里告诉我,谢谢。