gpt4 book ai didi

c# - GetAsync 请求返回空内容

转载 作者:行者123 更新时间:2023-12-05 06:36:10 26 4
gpt4 key购买 nike

我想知道为什么当我在 postman 中尝试它有数据返回时它返回空值。这是我的代码。

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;
}
}

下图是内容为null,状态码为true的httpresponse

enter image description here

这是 Postman 的回复 enter image description here

我也对 web API 做了断点,因为它只在我的本地然后它实际上有数据。

最佳答案

避免混合异步/等待和阻塞调用,如 .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 应该如何使用。不建议为每个请求创建一个新实例。

关于c# - GetAsync 请求返回空内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49247091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com