在C#中使用REST API将附件发布到Jira [英] Post attachment to Jira using REST API in c#

查看:156
本文介绍了在C#中使用REST API将附件发布到Jira的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.当我想使用REST API发布JIRA的某些附件时,我遇到了带有404代码的Web异常(未找到).我的方法采用CookieContainer对用户进行身份验证.这是我的代码:

I Have a problem. When I want to POST some attachment to JIRA using REST API i have a Web Exception with 404 code (not found). My method takes CookieContainer to authenticate User. This is my code:

 HttpWebResponse response;
        string path = "C:\\Users\\xxxx\\Documents\\nowy.txt";
        var boundary = string.Format("----------{0:N}", Guid.NewGuid());
        request.CookieContainer = new CookieContainer();
        foreach (Cookie c in responseCookies)
            request.CookieContainer.Add(c);
        request.ContentType = String.Format("multipart/form-data; boundary={0}", boundary);
        request.Method = "POST";
        request.Headers.Add("X-Atlassian-Token: nocheck file=@my_file.txt");
        request.Headers.Add("charset", "UTF-8");
        request.KeepAlive = false; 

        var fileContent = File.ReadAllBytes(path);

        request.ProtocolVersion = HttpVersion.Version10;
        using(var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            streamWriter.Write(fileContent);
        }


        response = request.GetResponse() as HttpWebResponse;
        Stream s = response.GetResponseStream();

我曾经获得过很多支持,但是任何解决方法都可以解决我的问题.那你有什么主意吗?

I used for many support, but any resolved my problem. So do you have some idea?

编辑1

现在Jira返回了200条代码,但未添加附件..您能告诉我我的代码有什么问题吗?

now Jira returns 200 code but attachment was not added.. can you tell me what's wrong in my code?

public void AddAtachment(CookieCollection responseCookies)
    {
        request.CookieContainer = new CookieContainer();
        foreach (Cookie c in responseCookies)
            request.CookieContainer.Add(c);
        Console.Write(request.RequestUri);
        HttpWebResponse response;
        string fileUrl = @"C:\Users\xxxx\Documents\nowy.txt";
        var boundary = string.Format("----------{0:N}", Guid.NewGuid());


        request.ContentType = String.Format("multipart/form-data; boundary={0}", boundary);
        request.Method = "POST";
        request.Headers.Add("X-Atlassian-Token", "nocheck");

        MemoryStream postDataStream = new MemoryStream();
        StreamWriter postDataWriter = new StreamWriter(postDataStream);

        postDataWriter.Write("\r\n--" + boundary + "\r\n");
        postDataWriter.Write("Content-Disposition: form-data;"
                    + "@file=\"{0}\";"
                    + "filename=\"{1}\""
                    + "\r\nContent-Type: {2}\r\n\r\n",
                    "myFile",
                    Path.GetFileName(fileUrl),
                    Path.GetExtension(fileUrl));
        postDataWriter.Flush();

        FileStream fileStream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            postDataStream.Write(buffer, 0, bytesRead);
        }

        fileStream.Close();

        postDataWriter.Write("\r\n--" + boundary + "--\r\n");
        postDataWriter.Flush();

        request.ContentLength = postDataStream.Length;

        using (Stream s = request.GetRequestStream())
        {
            postDataStream.WriteTo(s);
        }

        postDataStream.Close();

        response = request.GetResponse() as HttpWebResponse;
        StreamReader responseReader = new StreamReader(response.GetResponseStream());
        string reply = responseReader.ReadToEnd();
        Console.Write(reply);
    }

推荐答案

据我所知,您的问题出在Content-Disposition标头中. RFC-1867 要求表单数据名称为文件" ,仅此而已.有关更多详细信息,请参阅Atlassian论坛上的帖子: https://answers.atlassian.com/questions/104822/jira-rest-api-attachment-incoherent-result .

From what I can see, your problem is in the Content-Disposition header. RFC-1867 expects the form-data name to be "file" and nothing else. For more details, look at the post on Atlassian Forums: https://answers.atlassian.com/questions/104822/jira-rest-api-attachment-incoherent-result.

您可以查看我在重复帖子中提供的答案: https://stackoverflow.com/a/18489214/424059

You may take a look at my answer provided on the Duplicate post: https://stackoverflow.com/a/18489214/424059

这篇关于在C#中使用REST API将附件发布到Jira的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆