如果只是拨打电话,你如何消费REST API
?
我正在使用Clickatell的REST API。我想使用C#
和HttpClient
,因为它是进行REST调用的最新和最好的方法。我正试图"转换"要在HttpClient中使用的调用。我设法做了一些,但不知道如何做其余的事。
卷曲:
curl -i \
-X GET \
-H "X-Version: 1" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Your Authorization Token" \
-H "Accept: application/json" \
-s \
https://api.clickatell.com/rest/account/balance
这就是我目前所拥有的:
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("https://api.clickatell.com/rest/account/balance");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "my_cliackatell_token");
httpClient.DefaultRequestHeaders.Add("X-Version", "1");
}
我不确定以下内容是什么以及如何在上述代码中实现它们:
-i \
-H "Accept: application/json" \
-s \
如何在POST时完成上述内容?
这两个设置是一样的吗? -H "Content-Type: application/json"
和-H "Accept: application/json"
答案 0 :(得分:0)
你可以按如下方式使用get方法调用(这只是一个例子)
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:56851/");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/User").Result;
if (response.IsSuccessStatusCode)
{
// do logic
}
else
{
MessageBox.Show("Error Code" +
response.StatusCode + " : Message - " + response.ReasonPhrase);
}
Refer the below link to learn more about Httpclient
希望这有帮助