2011-12-21 79 views
1

我試圖單元測試一些基本的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

謝謝。

+1

也許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

回答

1

默認情況下,Mono未附帶任何受信任的根證書。

可以使用mozroot自行安裝,或者只是爲了測試(生產),使用allow any SSL證書。

+0

感謝您的回覆。我已經閱讀過這篇文章,並不認爲它適用。 HTTPS代碼在單聲道控制檯應用程序中正常工作。它在單元測試中不起作用。 – 2011-12-22 23:40:56