gpt4 book ai didi

c# - 尝试通过 .NET REST API 发布

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:44 25 4
gpt4 key购买 nike

所以我正在编写一个 C#.NET 应用程序,它需要能够从 Rally 读取历史数据。具体来说,我需要检索给定版本在发布前 7 天的每一天的总计划估算。

我读到基本上有两种方法可以做到这一点;通过修订历史或通过 Lookback API 并使用 POST。我认为 LBAPI 会更干净、更健壮,所以我选择了这条路。不幸的是,我以前从未使用过 POST,所以我不完全确定自己在做什么。

这是我到目前为止的想法。我觉得我要么很接近并且错过了一些非常明显的东西,要么我完全离开了:

string uri = @"https://rally1.rallydev.com/analytics/v2.0/service/rally/workspace/14457696030/artifact/snapshot/query.js";

DynamicJsonObject data = new DynamicJsonObject();
data["find"] = new DynamicJsonObject();
data["find"]["_ProjectHierarchy"] = 14457702297;
data["find"]["_TypeHierarchy"] = "HierarchicalRequirement";
data["find"]["Children"] = null;
data["find"]["__At"] = "2014-02-02T00Z%22";
data["fields"] = new string[] { "PlanEstimate", "ObjectID", "Name" };

DynamicJsonObject results = API.post(uri, data);

当我在网络浏览器中使用编译后的 url 时,我得到了正确的数据:

https://rally1.rallydev.com/analytics/v2.0/service/rally/workspace/14457696030/artifact/snapshot/query.js?find={%22_ProjectHierarchy%22:14457702297,%22_TypeHierarchy%22:%22HierarchicalRequirement%22,%22Children%22:null,%22__At%22:%222014-02-02T00Z%22}&fields=[%22ObjectID%22,%22Name%22,%22PlanEstimate%22]&start=0

当我使用上面的 C# 代码并调用时:

DynamicJsonObject results = API.post(uri, data);

我收到一个 WebException 声明:“远程服务器返回了一个错误:(405) 不允许的方法。” 这听起来好像我什至无法使用该 post 方法开始。

任何指导将不胜感激。

最佳答案

如果其他人想要做类似的事情,这是我在 Mark 的指导下的解决方案(可能做得更干净,但这对我有用):

    private static DynamicJsonObject getBaseline(string day, long projectID, long ReleaseID)
{
int pageSize = 200;
string uri = @"https://rally1.rallydev.com/analytics/v2.0/service/rally/workspace/14457696030/artifact/snapshot/query.js?find={%22_ProjectHierarchy%22:" +
projectID + @",%22_TypeHierarchy%22:%22HierarchicalRequirement%22,%22Release%22:{$in:[" + ReleaseID + @"]},%22Children%22:null,%22__At%22:%22" +
day + @"T00Z%22}&fields=[%22PlanEstimate%22,%22ScheduleState%22]&hydrate=[%22ScheduleState%22]&start=0&pagesize=" + pageSize;

DynamicJsonObject response = HttpGet(uri);
DynamicJsonObject points = new DynamicJsonObject();

foreach (var story in response["Results"])
{
// Do whatever
}
return points;
}

public static DynamicJsonObject HttpGet(string URI)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Credentials = GetCredential();
req.PreAuthenticate = true;
req.ContentType = "application/json";
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());

DynamicJsonObject result = new DynamicJsonSerializer().Deserialize(sr.ReadToEnd());
// Not sure if all of these are necessary
sr.Close(); sr.Dispose();
resp.Close();
sr.Close(); sr.Dispose();
return result;
}

private static CredentialCache GetCredential()
{
if (credentialCache == null)
{
string url = @"https://rally1.rallydev.com/analytics/v2.0/service/rally/workspace/14457696030/artifact/snapshot/query.js?";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
credentialCache = new CredentialCache();
String user = ConfigurationManager.AppSettings["user"];
String password = ConfigurationManager.AppSettings["password"];
credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(user, password));
}
return credentialCache;
}

关于c# - 尝试通过 .NET REST API 发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22801630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com