[英]HttpClient - body content is not returned?
响应始终为null,但是当通过curl进行api调用时,将返回正文内容。
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:5008/Accounts/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ACCESSTOKENHERE");
//GET Method
HttpResponseMessage httpResponse = await client.GetAsync("searchKeyword=" + searchKeyword);
try
{
AccountListResponse response = await httpResponse.Content.ReadAsAsync<AccountListResponse>();
}
catch (Exception)
{
}
}
我只能为您提供故障排除帮助,因为您的问题不包含来自api调用的示例响应。
要进行故障排除,请以字符串形式读取响应。 然后检查您是否得到回应。 以及statusCode。
.Content.ReadAsStringAsync()
如果能给出结果。 然后,您的AccountListResponse
没有正确的响应映射。
使用json to c#方法生成正确的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.