【问题标题】:When attach a file to JIRA issue attachment using HTTPClient Post method, JIRA return JSON object is "[]"使用 HTTPClient Post 方法将文件附加到 JIRA 问题附件时,JIRA 返回 JSON 对象为“[]”
【发布时间】:2013-06-15 06:40:03
【问题描述】:

我尝试使用 HttpClient post 方法将文件附加到 JIRA 问题附件 - 返回的 JIRA JSON 对象是 []。请在下面找到我的代码块。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Json;
using System.Web.Script.Serialization;
using System.Net;

namespace JiraAttachements
{
    class Class1
    {
        public void AddAttachment()
        {
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            client.DefaultRequestHeaders.ExpectContinue = false;
            client.Timeout = TimeSpan.FromMinutes(90);
            byte[] crdential = UTF8Encoding.UTF8.GetBytes("wwww:yyyy");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(crdential));
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            var filecontent = new ByteArrayContent(System.IO.File.ReadAllBytes("C:\\Users\\xxx\\Desktop\\Keys.txt"));

            var content = new MultipartFormDataContent("AA");

            content.Headers.Add("X-Atlassian-Token", "nocheck");
            content.Headers.Add("charset", "UTF-8");

            content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
            {
                Name="\"file\"",
                FileName = "C:\\Users\\xxx\\Desktop\\Keys.txt"
            };

            content.Add(filecontent);
            try
            {
                client.PostAsync("https://{server name}.atlassian.net/rest/api/2/issue/TEST-1/attachments", content).ContinueWith(requesTask =>
                {
                    try
                    {
                        HttpResponseMessage response = requesTask.Result;
                        if (response.StatusCode == "OK")
                        {
                            Console.WriteLine(" Attached .");
                        }
                        else
                        {
                        }
                    }
                    catch (Exception exception)
                    {
                    }
                });


            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.StackTrace.ToString());
                Console.ReadLine();
            }

            Console.ReadKey();
        }
    }
}

请在我的代码中突出显示我的错误。我对 multipart/form-data 边界值设置过程感到震惊。请给出一些使用HttpClient 发布方法的示例,用于 JIRA 问题附件。

【问题讨论】:

标签: c# httpclient jira


【解决方案1】:

现在我可以通过更改以下代码块将文件附加到 JIRA 问题附件部分,

var filename = "C:\\Users\\XXXX\\Desktop\\Sample.xlsx";
var file_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var filecontent = new ByteArrayContent(System.IO.File.ReadAllBytes(filename));
var content = new MultipartContent("form-data", "AAAA");

content.Headers.Add("X-Atlassian-Token", "nocheck");
content.Headers.Add("charset", "UTF-8");

filecontent.Headers.ContentDisposition = 
    new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") {
        Name="\"file\"",
        FileName = "Attachment.xlsx"
    };

filecontent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file_type);
content.Add(filecontent);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-19
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多