0

我是使用基于 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 问题磁贴和描述等)的最佳方式是什么?

非常感谢

保罗

4

1 回答 1

0

You can avoid this error by following the instructions posted here: http://forums.asp.net/post/4845421.aspx.

However, it is advisable to use a Json.NET based formatter which will ship out of the box upon the next drop of ASP.NET Web API. If you're not willing to wait and comfortable compiling the code yourself, you can get it now by pulling latest from Microsoft's open source repository: http://aspnetwebstack.codeplex.com/. The formatter you want can specifically be found here: http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5ac8586b78b3#src%2fSystem.Net.Http.Formatting%2fFormatting%2fJsonMediaTypeFormatter.cs.

于 2012-05-22T05:10:26.627 回答