我正在尝试使用以下代码在我的 jira 中创建新项目问题,
using using System.Text;
using System.Net.Http;
using System.Json;
using System.Web.Script.Serialization;
namespace Test
{
class Class1
{
public void CreateIssue()
{
string message = "Hai \"!Hello\" ";
string data = "{\"fields\":{\"project\":{\"key\":\"TP\"},\"summary\":\"" + message + "\",\"issuetype\":{\"name\": \"Bug\"}}}";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
client.DefaultRequestHeaders.ExpectContinue = false;
client.Timeout = TimeSpan.FromMinutes(90);
byte[] crdential = UTF8Encoding.UTF8.GetBytes("adminName:adminPassword");
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"));
System.Net.Http.HttpContent content = new StringContent(data, Encoding.UTF8, "application/json");
try
{
client.PostAsync("http://localhost:8080/rest/api/2/issue",content).ContinueWith(requesTask=>
{
try
{
HttpResponseMessage response = requesTask.Result;
response.EnsureSuccessStatusCode();
response.Content.ReadAsStringAsync().ContinueWith(readTask =>
{
var out1 = readTask.Result;
});
}
catch (Exception exception)
{
Console.WriteLine(exception.StackTrace.ToString());
Console.ReadLine();
}
});
}
catch (Exception exc)
{
}
}
}
}
它抛出如下错误:
"{\"errorMessages\":[\"意外字符 ('!' (code 33)): 在 [Source: org.apache.catalina.connector.CoyoteInputStream@1ac4eeea; 行:1,列:52]\"]}"
但是我在 Firefox Poster 中使用了相同的 json 文件,我已经创建了这个问题。
Json 文件:{"fields":{"project":{"key":"TP"},"summary":"Hai \"!Hello\" ",\"issuetype\":{\"name\": \“漏洞\”}}}”
那么,我的代码有什么问题?