我遇到了一个问题,非常感谢您的帮助。 我试图在C#应用程序中执行curl命令,但得到的响应结果是标题本身,但输出中实际上缺少我需要的Json内容。似乎无法弄清楚为什么输出中缺少Json字符串的实际内容。如果我手动执行curl命令,我将毫无问题地收到json内容。
我错过了什么?
下面提供了代码参考
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "http://10.10.100.11:8080/ords/krauta/oauth/token"))
{
var base64Authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("Bam1EfR6yasT1pJlhOzJmQ..:T6SnqCHsa90dm6wu_l3-2g.."));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64Authorization}");
request.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
var response = httpClient.SendAsync(request);
Console.Write(response.Result);
}
}
答案 0 :(得分:0)
在乔的帮助下找到了解决问题的方法。 这是下面的代码,如果将来有人需要引用的话。
private static async Task GetAsyncToken()
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "http://10.10.100.11:8080/ords/krauta/oauth/token"))
{
var base64Authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("Bam1EfR6yasT1pJlhOzJmQ..:T6SnqCHsa90dm6wu_l3-2g.."));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64Authorization}");
request.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await httpClient.SendAsync(request);
var result = await response.Content.ReadAsStringAsync();
var parseTokenValue = ParseToken.FromJson(result);
_tokenValue = parseTokenValue.AccessToken;
}
}
}