【问题标题】:How do I use the Redmine REST API over https from .NET?如何通过 .NET 的 https 使用 Redmine REST API?
【发布时间】:2012-05-15 08:51:21
【问题描述】:

我们的内部 Redmine 服务器只允许我通过 HTTPS 连接。以下是我尝试通过 .NET 中的 HTTPS 使用 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 中的 Hangs here 注释)。然后我尝试从 Redmine 服务器下载其他内容(例如 time_entries.csv、atom feed 等),每次都得到完全相同的结果。

到目前为止很糟糕。但是,如果我将 URL https://redmine.company.com/redmine/issues/149.xml?key=ffffffffffffffffffffffffffffffffffffffff 复制粘贴到我的浏览器中,我会得到我期望的响应。所以,看起来我们的 Redmine 服务器的行为应该是正常的,但不知何故我无法让它在 .NET 上工作。

我已经成功地从其他 HTTPS 站点下载了东西,并设法使用尝试 2 的代码从http://demo.redmine.org 下载了问题数据(当然还有经过调整的 URL 等)。因此,Redmine 如何通过 HTTPS 进行通信似乎有些特别。

如果有人通过 .NET 的 HTTPS 成功使用 Redmine REST API,我将非常感谢您指出我做错了什么。

此外,我们将不胜感激有关如何从客户端进行调试的建议。到目前为止,我已经尝试过 Fiddler2,但没有成功。一旦我启用它的“解密 HTTPS 流量”设置,当我在 Internet Explorer 中发出请求时,我就不再得到答案。

【问题讨论】:

    标签: c# .net redmine


    【解决方案1】:

    我们使用redmine-net-api,它支持基于 API 密钥的 HTTP/S 连接和身份验证。

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

    【讨论】:

    • 我刚刚尝试过使用您的代码,替换了 。我得到与其他尝试完全相同的结果(超时)。您是否通过 https 使用 API?谢谢!
    • 嘿,您是否成功获得了结果。如果我尝试此操作,我会得到名称不同于参数列表 IList issues = manager.GetObjectList(new NameValueCollection () { { "tracker_name", "Client Requests" } });
    【解决方案2】:

    试试这个,它对我有用:

    // 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"] = ....
    }
    

    【讨论】:

      【解决方案3】:

      securityProtocolType 参数显式传递SecurityProtocolType.Tls12 值解决了我的问题:

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

      【讨论】:

        猜你喜欢
        • 2016-07-07
        • 1970-01-01
        • 2014-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多