2012-05-15 53 views
0

我們內部的Redmine服務器只允許我通過HTTPS進行連接。以下是我試圖通過HTTPS從.NET使用REST API:如何通過https從.NET使用Redmine REST API?

  1. 作爲Using the REST API with .NET建議,設置主機變量"https://redmine.company.com/redmine/"和apiKey到"ffffffffffffffffffffffffffffffffffffffff"
  2. 從無到有用下面的代碼:

    using System.IO; 
    using System.Net; 
    
    class Program 
    { 
        static void Main(string[] args) 
        { 
         ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) => true; 
    
         var request = (HttpWebRequest)WebRequest.Create(
          "https://redmine.company.com/redmine/issues/149.xml?key=ffffffffffffffffffffffffffffffffffffffff"); 
         request.CookieContainer = new CookieContainer(); 
         request.Method = "GET"; 
    
         using (var response = request.GetResponse()) // Hangs here 
         using (var responseStream = response.GetResponseStream()) 
         using (var memoryStream = new MemoryStream()) 
         { 
          responseStream.CopyTo(memoryStream); 
         } 
        } 
    } 
    

當然,company.comffffffffffffffffffffffffffffffffffffffff是我真實的公司和我的帳戶頁面上我真正的API密鑰只是佔位符。在WebException超時之前,這兩種嘗試都會暫停一段時間(請參閱在嘗試2中掛起評論)。然後我嘗試從Redmine服務器上下載其他東西(例如time_entries.csv,atom feed等),每次都得到完全相同的結果。

到目前爲止那麼糟糕。但是,如果我將URL https://redmine.company.com/redmine/issues/149.xml?key=ffffffffffffffffffffffffffffffffffffffff複製粘貼到我的瀏覽器中,我就會得到我期望的響應。所以,好像我們的Redmine服務器的行爲應該如此,但不知何故,我無法從.NET中使用它。

我已成功從其他HTTPS網站下載了內容,並已嘗試從http://demo.redmine.org下載問題數據,其中包含嘗試2的代碼(當然還包括改編的URL等)。因此,Redmine如何通過HTTPS進行通信似乎有些特別之處。

如果有人成功使用從.NET Redmine的REST API通過HTTPS,我是什麼,我做錯了一些三分球真的很感激。

此外,關於如何從客戶端調試這個建議,將不勝感激。到目前爲止,我已經嘗試過Fiddler2,沒有成功。只要我啓用其「解密HTTPS流量」設置,當我在Internet   Explorer中提出請求時,我將不再得到答案。

回答

1

我們使用redmine-net-api它支持HTTP/S連接和認證基於API密鑰。


     RedmineManager rm = new RedmineManager("https://&ltyour-address>", &ltapi-key>, "random-password"); 
     IList&ltIssue> issues = rm.GetObjectList&ltIssue>(new NameValueCollection() { { "project_id", &ltproject-id> } }); 
+0

,替換。我得到和我的其他嘗試完全相同的結果(超時)。你是否通過https使用API​​?謝謝! –

+0

嘿你在得到result.If成功我試試這個即時得到任務比在參數列表中的IList 問題= manager.GetObjectList (新NameValueCollection中(){{「tracker_name」,「客戶端請求給出的另外一個名字「}}); – Vivekh

0

試試這個,它爲我工作:

// Allow every secure connection 
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) => true; 

// Create redmine manager (where URL is "https://10.27.10.10/redmine" for me and redmineKey is my redmine API key 
RedmineManager redmineManager = new RedmineManager(redmineURL, redmineKey); 

// Create your query parameters 
NameValueCollection queryParameters = new NameValueCollection { { "project_id", "4" }, {"tracker_id", "17"}, { "offset", "0" } }; 

// Perform your query 
int issuesFound = 0; 
foreach (var issue in redmineManager.GetObjectList<Issue>(queryParameters, out issuesFound)) 
{ 
// By default you get the 25 first issues of the project_id and tracker_id specified. 
// Play with the offset to get the rest 
queryParameters["offset"] = .... 
} 
0

securityProtocolType參數明確傳遞SecurityProtocolType.Tls12值解決了我的情況的問題:我只是試着用你的代碼

RedmineManager redmineManager = new RedmineManager(_host, _apiKey, 
    securityProtocolType: SecurityProtocolType.Tls12);