C# 如何通过.NET中的https使用Redmine REST API?
我们的内部Redmine服务器只允许我通过HTTPS进行连接。下面是我如何尝试从.NET通过HTTPS使用REST API的:C# 如何通过.NET中的https使用Redmine REST API?,c#,.net,redmine,C#,.net,Redmine,我们的内部Redmine服务器只允许我通过HTTPS进行连接。下面是我如何尝试从.NET通过HTTPS使用REST API的: 按照中的建议,将主机变量设置为”https://redmine.company.com/redmine/“和的apiKey”ffffffffffffffffffffffffffffffffff“ 使用以下代码从头开始: using System.IO; using System.Net; class Program { static void Main(str
”https://redmine.company.com/redmine/“
和的apiKey”ffffffffffffffffffffffffffffffffff“
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.com
和ffffffffffffffffffffffffffffffffffff
只是我账户页面上我真正的公司和我真正的API密钥的占位符。两次尝试都会挂起一段时间,然后超时并出现WebException(请参阅尝试2中的“挂起此处”注释)。然后,我尝试从Redmine服务器下载其他内容(如time_entries.csv、atom提要等),每次结果都完全相同
到目前为止还很糟糕。但是,如果我复制粘贴URLhttps://redmine.company.com/redmine/issues/149.xml?key=ffffffffffffffffffffffffffffffffffffffff
在我的浏览器中,我得到了预期的准确响应。所以,看起来我们的Redmine服务器的行为应该是正常的,但不知怎么的,我无法从.NET让它工作
我已经成功地从其他HTTPS站点下载了内容,并成功地用尝试2的代码(当然是经过修改的URL等)从中下载了问题数据。因此,Redmine通过HTTPS进行通信的方式似乎有一些特殊之处
如果有人在.NET的HTTPS上成功地使用了Redmine REST API,我会非常感激一些关于我做错了什么的指点
此外,对于如何从客户端调试此问题的建议,我们将不胜感激。到目前为止,我试过小提琴2,但没有成功。一旦我启用了“解密HTTPS流量”设置,那么当我在InternetExplorer中发出请求时,我就不会再得到答复。我们使用它,它支持HTTP/S连接和基于API密钥的身份验证
RedmineManager rm = new RedmineManager("https://<your-address>", <api-key>, "random-password");
IList<Issue> issues = rm.GetObjectList<Issue>(new NameValueCollection() { { "project_id", <project-id> } });
试试这个,它对我有用:
// 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"] = ....
}
//允许每个安全连接
ServicePointManager.ServerCertificateValidationCallback+=(发件人、证书、链、错误)=>true;
//创建redmine管理器(其中URL为“https://10.27.10.10/redmine“对于我来说,redmineKey是我的redmine API密钥
RedmineManager RedmineManager=新的RedmineManager(redmineURL,redmineKey);
//创建查询参数
NameValueCollection queryParameters=新的NameValueCollection{{{“项目id”,“4”},{“跟踪器id”,“17”},{“偏移量”,“0”};
//执行您的查询
int issuesFound=0;
foreach(redmineManager.GetObjectList中的var问题(queryParameters,OutIssuesFound))
{
//默认情况下,您将获得指定的项目id和跟踪器id的前25个问题。
//使用偏移量来获得其余部分
查询参数[“偏移量”]=。。。。
}
显式传递SecurityProtocolType.Tls12
SecurityProtocolType参数的值解决了我的案例中的问题:
RedmineManager redmineManager = new RedmineManager(_host, _apiKey,
securityProtocolType: SecurityProtocolType.Tls12);
我刚刚尝试使用您的代码、替换和。我得到的结果(超时)与其他尝试完全相同。您是否通过https使用API?谢谢!嘿,您是否成功获得了结果。如果我尝试此操作,我将获得名称不同于参数列表IList issues=manager.GetObjectList中给定名称的任务(新的NameValueCollection(){{“跟踪器名称”,“客户端请求”});