【发布时间】:2019-07-03 07:55:48
【问题描述】:
我正在尝试将来自 Jira API 的 curl 请求示例转换为 C# 请求。
这是 JIRA 提供的原始 Curl Request 示例:
curl \
-D- \
-u charlie:charlie \
-X GET \
-H "Content-Type: application/json" \
http://localhost:8080/rest/api/2/search?jql=assignee=charlie
我已将其翻译成以下 JIRA 代码:
但是响应行不起作用 - 因为我拼凑了一些示例并且有点卡住了!
var myTask = curlRequestAsync(); // call your method which will return control once it hits await
string result = myTask.Result();
// .Result isn't defined - but I'm not sure how to access the response from my request!
任何帮助将不胜感激,因为我真的被困住了!
完整示例:
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var myTask = curlRequestAsync(); // call your method which will return control once it hits await
string result = myTask.Result();
// wait for the task to complete to continue
}
protected async System.Threading.Tasks.Task curlRequestAsync()
{
try
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("GET"), "http://myurl.co.uk/rest/api/2/search?jql=assignee=bob"))
{
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
var result = await httpClient.SendAsync(request);
return result;
}
}
}
catch (Exception ex)
{
error.InnerText = ex.Message;
}
}
}
【问题讨论】:
-
你可能应该使用
var result = await curlRequestAsync();
标签: c# .net asynchronous jira dotnet-httpclient