我想知道为什么当我在postman中尝试它有数据返回时它返回null。这是我的代码。
public async Task<List<HomeViewModel>> GetUserJobs(LoginResult token)
{
var result = new List<HomeViewModel>();
string url = "JobHeaders?userName=" + token.userName;
HttpResponseMessage httpResponse = await _requestHelper.GetHttpResponse(url, token.access_token);
result = GetJobResponse(httpResponse);
return result;
}
获取异步功能
public async Task<HttpResponseMessage> GetHttpResponse(string requestUri, string token)
{
using (var client = CreateHttpClient(token))
{
try
{
HttpResponseMessage response = await client.GetAsync(requestUri);
return response;
}
catch (HttpRequestException ex)
{
throw new HttpRequestException(ex.Message);
}
}
}
添加基地
private HttpClient CreateHttpClient(string authorization="")
{
var client = new HttpClient();
string baseAddress = WebApiBaseAddress;
if (string.IsNullOrEmpty(baseAddress))
{
throw new HttpRequestException("There is no base address specified in the configuration file.");
}
client.Timeout = new TimeSpan(0, 5, 59);
client.BaseAddress = new Uri(baseAddress);
//client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Authorization", string.Format("bearer {0}", authorization));
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("LegacyUse", "true");
return client;
}
反序列化JSON
private List<HomeViewModel> GetJobResponse(HttpResponseMessage response)
{
var result = new List<HomeViewModel>();
if (response.IsSuccessStatusCode)
{
var jsonString = response.Content.ReadAsStringAsync();
jsonString.Wait();
if (jsonString.IsCompleted)
{
try
{
var data = JObject.Parse(jsonString.Result);
result = JsonConvert.DeserializeObject<List<HomeViewModel>>(data.ToString());
result.FirstOrDefault().isSuccess = true;
result.FirstOrDefault().TotalJobs = result.Count();
result.FirstOrDefault().TotalOpenJobs = result.Where(x => x.JobStatus == "Picked_Up" || x.JobStatus == "Picked Up").ToList().Count();
}
catch (Exception ex)
{
result.FirstOrDefault().message = ex.Message;
result.FirstOrDefault().isSuccess = false;
}
}
return result;
}
else
{
return null;
}
}
下面的图片是httpresponse,其中内容为null且状态代码为true
我也断断了Web API,因为它只在我的本地,然后它实际上有数据。
答案 0 :(得分:2)
避免混合async / await和阻止.Wait()
或.Result
将代码重构为异步
private async Task<List<HomeViewModel>> GetJobResponse(HttpResponseMessage response) {
var result = new List<HomeViewModel>();
if (response.IsSuccessStatusCode) {
var jsonString = await response.Content.ReadAsStringAsync();
try {
result = JsonConvert.DeserializeObject<List<HomeViewModel>>(jsonString);
if(result != null && result.Count > 0) {
result.FirstOrDefault().isSuccess = true;
result.FirstOrDefault().TotalJobs = result.Count();
result.FirstOrDefault().TotalOpenJobs = result.Where(x => x.JobStatus == "Picked_Up" || x.JobStatus == "Picked Up").Count();
}
} catch (Exception ex) {
var item = new HomeViewModel {
message = ex.Message,
isSuccess = false
};
result.Add(item);
}
return result;
} else {
return null;
}
}
并使用
public async Task<List<HomeViewModel>> GetUserJobs(LoginResult token) {
var result = new List<HomeViewModel>();
string url = "JobHeaders?userName=" + token.userName;
HttpResponseMessage httpResponse = await _requestHelper.GetHttpResponse(url, token.access_token);
result = await GetJobResponse(httpResponse);
return result;
}
最后,我建议您查看应该如何使用HttpClient
。不建议为每个请求创建新实例。
答案 1 :(得分:-1)
您的回复 有内容。它是“本地”窗口中图像的第二行。这是一个StreamContent
对象。
您所看到的是请求的内容。这没有内容,因为HTTP GET请求没有正文(通常)。