【问题标题】:Get JIRA issue using JIRA 5.0.1 REST JSON API using a .NET client使用 .NET 客户端使用 JIRA 5.0.1 REST JSON API 获取 JIRA 问题
【发布时间】:2023-03-28 09:07:01
【问题描述】:

我是使用基于 REST 的服务的新手,但我想做的是我希望很容易的事情,即获取 JIRA 问题并使用 .NET Framework 4.5 客户端显示它。

我发现可以通过将以下 URI 粘贴到浏览器中来获得 JSON 响应: https://jira.atlassian.com/rest/api/latest/issue/JRA-9

我需要做的是从 .net 应用程序中调用它。因此,经过一些研究,我发现在 .NET 4.5 中使用 HTTPClient 是可行的方法。

为了运行以下测试代码,您需要引用 .NET Framework 4.5 并添加对 System.Net.Http 和扩展 System.Net.Http 和 System.Json 的引用:

using System;
using System.Collections.Generic;
using System.Json;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace JIRAClient3
{
    class Program
    {
        static string _address = "https://jira.atlassian.com/rest/api/latest/issue/JRA-9";

        static async void Run()
        {
            // Create an HttpClient instance
            HttpClient client = new HttpClient();
            client.MaxResponseContentBufferSize = int.MaxValue;

            // Send a request asynchronously continue when complete
            HttpResponseMessage response = await client.GetAsync(_address);

            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();

            // Read response asynchronously as JsonValue
            JsonArray content = await response.Content.ReadAsAsync<JsonArray>();

            // Exception occurs at the above line, which is:
            //System.InvalidOperationException was unhandled by user code
            //HResult=-2146233079
            //Message=The input stream contains too many delimiter characters which may 
            //be a sign that the incoming data may be malicious.
            //Source=System.Net.Http.Formatting

            // I then need to write out the contents of the JSON/JIRA issue.
      }

        static void Main(string[] args)
        {
             Run();
             Console.WriteLine("Hit ENTER to exit...");
             Console.ReadLine();
        }
    }
}

所以我陷入了与太多分隔符有关的例外情况。因此,我的问题是:

  • 我是否正朝着正确的方向前进?
  • 如何解决分隔符过多异常。
  • 解决异常后,访问部分 JSON 响应(例如 JIRA 问题磁贴和描述等)的最佳方式是什么?

非常感谢

保罗

【问题讨论】:

    标签: json jira asp.net-web-api dotnet-httpclient


    【解决方案1】:

    您可以按照此处发布的说明避免此错误:http://forums.asp.net/post/4845421.aspx

    但是,建议使用基于 Json.NET 的格式化程序,该格式化程序将在下一次发布 ASP.NET Web API 时开箱即用。如果您不愿意等待并且不愿意自己编译代码,您现在可以通过从 Microsoft 的开源存储库中提取最新的代码来获取它:http://aspnetwebstack.codeplex.com/。您想要的格式化程序可以在这里找到:http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5ac8586b78b3#src%2fSystem.Net.Http.Formatting%2fFormatting%2fJsonMediaTypeFormatter.cs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2023-02-10
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多