C# Jira服务器中使用C的Jira Rest API登录错误#

C# Jira服务器中使用C的Jira Rest API登录错误#,c#,server,jira,C#,Server,Jira,我想使用C#Rest api连接到jira服务器 远程服务器返回错误(404) 我尝试了不同的解决办法,但都白费了。我可以知道为什么会出现这个错误吗?此错误的解决方法是什么?您可以通过不同的方式搜索此错误的原因: 通过查看JIRA服务器的日志,可能会有一些消息/堆栈跟踪(例如,atlasian JIRA.log) 通过使用一些工具来执行/调试/测试REST调用(例如,postman),当它开始在工具中工作时,您可以编写代码以编程方式完成它。JIRA可以在响应中返回错误描述,工具可以向您显示它

我想使用C#Rest api连接到jira服务器

远程服务器返回错误(404)


我尝试了不同的解决办法,但都白费了。我可以知道为什么会出现这个错误吗?此错误的解决方法是什么?

您可以通过不同的方式搜索此错误的原因:

  • 通过查看JIRA服务器的日志,可能会有一些消息/堆栈跟踪(例如,atlasian JIRA.log)
  • 通过使用一些工具来执行/调试/测试REST调用(例如,postman),当它开始在工具中工作时,您可以编写代码以编程方式完成它。JIRA可以在响应中返回错误描述,工具可以向您显示它
当你得到这个信息,它可以给你确切的原因,为什么它不工作。当我遇到403错误时,因为超过了不成功登录尝试的阈值,我使用web浏览器登录到JIRA服务器(并输入captcha),然后我就可以通过应用程序代码获得会话

我可以使用邮递员通过以下方式从JIRA成功获取会话:
请求类型:POST
网址:
正文:
{“用户名”:“我的用户名”,“密码”:“我的密码”}


标题:内容类型:application/json

您可以执行以下操作:

namespace YOUR_NAME_SPACE
{
  public class jira
  {
    public static string createTicket(string url, string data)
    {
        try
        {
            var client = new System.Net.Http.HttpClient();
            string base64Credentials = GetEncodedCredentials();
            var header = new AuthenticationHeaderValue("Basic", base64Credentials);
            client.DefaultRequestHeaders.Authorization = header;

            var content = new StringContent(data, Encoding.UTF8, "application/json");
            var result = client.PostAsync(url, content).Result;
            var response = result.Content.ReadAsStringAsync().Result;
            // You can call putIssue if you want
            return response;
        }
        catch (System.Net.WebException ex)
        {
            Console.WriteLine("Exception Occurred" + " : {0}", ex.Message);
            throw;
        }
    }

    private static string GetEncodedCredentials()
    {
        string mergedCredentials = string.Format("{0}:{1}", "LOGIN", "PASSWD");
        byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
        return Convert.ToBase64String(byteCredentials);
    }

    public static string jiraSerialise(string project, string summary, string description, string issutype, string author)
    {
        JObject valuesToJson =
           new JObject(
               new JProperty("fields",
                   new JObject(
                       new JProperty("project",
                            new JObject(new JProperty("key", project))),
                       new JProperty("summary", summary),
                       new JProperty("description", description),
                       new JProperty("issuetype",
                            new JObject(new JProperty("name", issutype))),
                       new JProperty("assignee",
                            new JObject(new JProperty("name", author))))));
        return valuesToJson.ToString();
    }

    public static string putSerialize(string key, string value)
    {
        JObject valueToJson =
            new JObject(
                new JProperty(key, value));
        return valueToJson.ToString();
    }

    public static string putIssue(string response, string author, System.Net.Http.HttpClient client)
    {
        JObject jsonResponse = JObject.Parse(response);
        Dictionary<string, string> dictResponse = jsonResponse.ToObject<Dictionary<string, string>>();
        string issueUrl = dictResponse.Last().Value;
        string issueAssignee = issueUrl + "/assignee";
        var authorContent = new StringContent(author, Encoding.UTF8, "application/json");
        var authorResult = client.PutAsync(issueAssignee, authorContent).Result;
        var authorResponse = authorResult.Content.ReadAsStringAsync().Result;
        Console.WriteLine(authorResponse);
        return authorResponse;
    }
  }
 }
希望有帮助:)

namespace YOUR_NAME_SPACE
{
  public class jira
  {
    public static string createTicket(string url, string data)
    {
        try
        {
            var client = new System.Net.Http.HttpClient();
            string base64Credentials = GetEncodedCredentials();
            var header = new AuthenticationHeaderValue("Basic", base64Credentials);
            client.DefaultRequestHeaders.Authorization = header;

            var content = new StringContent(data, Encoding.UTF8, "application/json");
            var result = client.PostAsync(url, content).Result;
            var response = result.Content.ReadAsStringAsync().Result;
            // You can call putIssue if you want
            return response;
        }
        catch (System.Net.WebException ex)
        {
            Console.WriteLine("Exception Occurred" + " : {0}", ex.Message);
            throw;
        }
    }

    private static string GetEncodedCredentials()
    {
        string mergedCredentials = string.Format("{0}:{1}", "LOGIN", "PASSWD");
        byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
        return Convert.ToBase64String(byteCredentials);
    }

    public static string jiraSerialise(string project, string summary, string description, string issutype, string author)
    {
        JObject valuesToJson =
           new JObject(
               new JProperty("fields",
                   new JObject(
                       new JProperty("project",
                            new JObject(new JProperty("key", project))),
                       new JProperty("summary", summary),
                       new JProperty("description", description),
                       new JProperty("issuetype",
                            new JObject(new JProperty("name", issutype))),
                       new JProperty("assignee",
                            new JObject(new JProperty("name", author))))));
        return valuesToJson.ToString();
    }

    public static string putSerialize(string key, string value)
    {
        JObject valueToJson =
            new JObject(
                new JProperty(key, value));
        return valueToJson.ToString();
    }

    public static string putIssue(string response, string author, System.Net.Http.HttpClient client)
    {
        JObject jsonResponse = JObject.Parse(response);
        Dictionary<string, string> dictResponse = jsonResponse.ToObject<Dictionary<string, string>>();
        string issueUrl = dictResponse.Last().Value;
        string issueAssignee = issueUrl + "/assignee";
        var authorContent = new StringContent(author, Encoding.UTF8, "application/json");
        var authorResult = client.PutAsync(issueAssignee, authorContent).Result;
        var authorResponse = authorResult.Content.ReadAsStringAsync().Result;
        Console.WriteLine(authorResponse);
        return authorResponse;
    }
  }
 }
 string data = jira.jiraSerialise("lala", "nameVulnerabilty", "descriptionField", "Bug", "author");
 string url = "http://YOUR_URL/rest/api/2/issue/";                                           
 Console.WriteLine(jira.createTicket(url, data));