我读过 atlassian 上的一个答案https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response其中一位用户通过以下代码创建了 JIRA 问题。我对其进行了调整,但通过使用自建类问题出现了错误ObjectContent
Http.HttpContent content = new Http.ObjectContent<Issue>(data, jsonFormatter);
编译器不会接受它。有人知道为什么吗?
public string CreateJiraIssue()
{
string data= @"{ ""fields"": {
""project"":
{
""key"": ""HELP""
},
""summary"": ""Test Ticket"",
""description"": ""Creating of an issue using project keys and issue type names using the REST API"",
""issuetype"": {
""name"": ""Ticket""
},
""assignee"": { ""name"": ""user"" }
}
}";
string postUrl = "https://xxx.jira.com/rest/api/2/";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
client.BaseAddress = new System.Uri(postUrl);
byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<Issue>(data, jsonFormatter);
System.Net.Http.HttpResponseMessage response = client.PostAsync("issue", content).Result;
if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
return result;
}
else
{
return response.StatusCode.ToString();
}
并使用
namespace IOnotification_System
{
public class Issue
{
public Fields fields { get; set; }
public Issue()
{
fields = new Fields();
}
}
public class Fields
{
public Project project { get; set; }
public string summary { get; set; }
public string description { get; set; }
public Assignee assignee { get; set; }
public IssueType issuetype { get; set; }
public Fields()
{
project = new Project();
issuetype = new IssueType();
}
}
public class Project
{
public string key { get; set; }
}
public class IssueType
{
public string name { get; set; }
}
public class Assignee
{
public string name { get; set; }
}
}
EDIT
该消息清楚地表明 System.Net.Http.ObjectContent() 需要一个 Issue 对象作为其第一个参数。我预计之后会出现另一条消息,表示无法从字符串转换为问题。
您正在将字符串传递给需要 Issue 对象的方法。格式化程序用于将 Issue 对象转换为 Json 字符串。
您已经拥有该字符串,因此尝试转换它是没有意义的。仅当您有要转换为 Json 字符串的 Issue 实例时,才需要格式化程序。您可以使用字符串内容 http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx类并使用它的Headers http://msdn.microsoft.com/en-us/library/system.net.http.httpcontent.headers.aspx属性来添加客户端上尚未设置的任何标头,例如:
var content=new StringContent(data);
Original
错误消息是什么以及您使用的是什么类型的项目?这System.Net.Http.Formatting http://msdn.microsoft.com/en-us/library/system.net.http.formatting.mediatypeformatter%28v=vs.108%29.aspx命名空间是 ASP.NET Web API 的一部分。您正在构建 ASP.NET 应用程序、控制台应用程序还是其他应用程序?
除非您正在构建 ASP.NET 站点,否则此代码将无法工作。如果你唯一的问题是如何解析 Json 请求,只需使用另一个 Json 反序列化类即可。Json.NET http://james.newtonking.com/projects/json-net.aspx是一个非常受欢迎的选择。
无论如何,没有理由使用 Json 类将字符串转换为包含完全相同字符串的 HttpContent 对象。您可以使用字符串内容 http://msdn.microsoft.com/en-us/library/system.net.http.stringcontent.aspx类并使用它的Headers http://msdn.microsoft.com/en-us/library/system.net.http.httpcontent.headers.aspx属性来添加客户端上尚未设置的任何标头。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)