使用WebClient收到ConnectFailure单声道nunit测试异常
问题描述:
我试图单元测试一些基本的System.Net.WebClient代码来使用HTTPS端点下载字符串。无论如何,似乎在NUnit测试中使用HTTPS失败。我可以在NUnit测试中使用HTTP而没有问题。我可以在控制台应用程序中运行HTTPS代码,而不会出现问题。使用WebClient收到ConnectFailure单声道nunit测试异常
下面的代码...
[Test()]
public void TestWebclientHttp()
{
using (System.Net.WebClient wc = new System.Net.WebClient())
{
string data = wc.DownloadString("http://maps.googleapis.com/maps/api/geocode/json?address=alvis%20court&sensor=true");
System.Diagnostics.Debug.WriteLine(data);
}
}
[Test()]
public void TestWebClientHttps()
{
using (System.Net.WebClient wc = new System.Net.WebClient())
{
string data = wc.DownloadString("https://maps.googleapis.com/maps/api/geocode/json?address=alvis%20court&sensor=true");
System.Diagnostics.Debug.WriteLine(data);
}
}
这里的堆栈跟踪...
at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /private/tmp/monobuild/build/BUILD/mono-2.10.6/mcs/class/System/System.Net/HttpWebRequest.cs:828
at System.Net.HttpWebRequest.GetResponse() [0x0000e] in /private/tmp/monobuild/build/BUILD/mono-2.10.6/mcs/class/System/System.Net/HttpWebRequest.cs:836
at System.Net.WebClient.GetWebResponse (System.Net.WebRequest request) [0x00000] in /private/tmp/monobuild/build/BUILD/mono-2.10.6/mcs/class/System/System.Net/WebClient.cs:1433
at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x00000] in /private/tmp/monobuild/build/BUILD/mono-2.10.6/mcs/class/System/System.Net/WebClient.cs:866
at System.Net.WebClient.DownloadDataCore (System.Uri address, System.Object userToken) [0x0000a] in /private/tmp/monobuild/build/BUILD/mono-2.10.6/mcs/class/System/System.Net/WebClient.cs:246
开发环境...
- 的Mac OS X 10.6.8
- Mono develop 2.8.5
- 单声道运行时2.10.6
谢谢。
答
默认情况下,Mono未附带任何受信任的根证书。
可以使用mozroot
自行安装,或者只是为了测试(不生产),使用allow any SSL证书。
+0
感谢您的回复。我已经阅读过这篇文章,并不认为它适用。 HTTPS代码在单声道控制台应用程序中正常工作。它在单元测试中不起作用。 – 2011-12-22 23:40:56
也许http://www.mono-project.com/FAQ:_Security#WebRequest.Create_.28.22https:.2F.2Fwww.anywhere.com.22.29.3B_throws_an_exception – 2011-12-21 21:00:08