正在制作一个用户可以在其中发布问题以及更多内容的应用程序。我可以使用基本身份验证通过Postman发布问题,并发布此原始application / json
{
"fields": {
"project":
{
"key": "TEST"
},
"summary": "zfasf.",
"description": "Creating an issue while setting custom field values via postman",
"issuetype": {
"name": "Fault"
}
}
}
但是现在我必须尝试以C#Xamarin形式复制它。我无法发布它,因为它会出现以下错误:400错误的请求或“基本”值的格式无效。出于测试目的,“ JiraProblem”是具有诸如postman之类的属性的类。我的代码:
public async Task PostIssueAsync(JiraProblem problem, string password)
{
var httpClient = new HttpClient(new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (a, b, c, d) => true
});
var stringToBytes = Encoding.UTF8.GetBytes("<MyUsername>:" + password);
var JiraTokenBase = System.Convert.ToBase64String(stringToBytes);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic " + JiraTokenBase);
var json = JsonConvert.SerializeObject(problem);
HttpContent content = new StringContent(json);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await httpClient.PostAsync(Constants.JiraUrl + "rest/api/2/issue/", content);
}
此代码对我有用。
您可能需要Encoding.ASCII部分。
也可以将其转换为字节数组。
System.Net.WebClient cli = new System.Net.WebClient();
string authInfo = My.Settings.username + ":" + My.Settings.password;
cli.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo)));
cli.Headers.Add("Content-Type", "application/json");
var bytes = Encoding.Default.GetBytes(jsonstringpayload);
webClient.UploadDataAsyn("http://support.example.com:8080/rest/" + url, "POST", bytes);