【发布时间】:2019-08-22 10:39:54
【问题描述】:
我正在尝试使用来自 c#/.net 的令牌访问/调用 REST API 中的方法,但我无法得到任何响应。我用谷歌搜索了很多 - 但没有任何成功:-(我是通过 REST API 调用方法的新手。
我有一个端点和一个令牌,需要用于与 REST API 进行通信。我需要通过这些方法在服务器上获取、发布、放置和删除数据
API 的输出是 JSON 格式。
也许这很简单 - 但我不知道该怎么做。
感谢任何帮助。
我尝试了以下解决方案 - 但没有成功:-(
private static async void DoIt()
{
using (var stringContent = new StringContent("{ \"firstName\": \"Andy\" }", System.Text.Encoding.UTF8, "application/json"))
using (var client = new HttpClient())
{
try
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token);
// 1. Consume the POST command
var response = await client.PostAsync(endpoint, stringContent);
var result = await response.Content.ReadAsStringAsync();
//Console.WriteLine("Result from POST command: " + result);
// 2. Consume the GET command
response = await client.GetAsync(endpoint);
if (response.IsSuccessStatusCode)
{
var id = await response.Content.ReadAsStringAsync();
//Console.WriteLine("Result from GET command: " + result);
}
}
catch (Exception ex)
{
//Console.ForegroundColor = ConsoleColor.Red;
//Console.WriteLine(ex.Message);
//Console.ResetColor();
}
}
}
【问题讨论】:
标签: c# authentication token