B站视频WPF实战教程修改代码
P36
HttpRestClient.cs版本110.2 部分代码:
代码查询可以,但增加和更新会出问题,建议大家切换到老师的版本106.12。但问题不知道在哪里,有会的大佬可以帮我看下吗?
23.10更新:增加和更新也可以了
public async Task<ApiResponse> ExecuteAsync(BaseRequest baseRequest)
{
//uri
var uri = new Uri(apiUrl + baseRequest.Route);
//new restrequest
//add uri
var request = new RestRequest(uri);
//add method
request.Method=baseRequest.Method;
//add header
request.AddHeader("Content-Type", baseRequest.ContentType);
//add Parameter
if (baseRequest.Parameter != null)
request.AddParameter("param", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
//判断client是否为空
if (client == null)
return new ApiResponse()
{
Status = false,
Result = null,
Message = "client为空"
};
//执行
var response = await client.ExecuteAsync(request);
//如果成功则执行序列化任务,否则返回失败代码
if (response.StatusCode == System.Net.HttpStatusCode.OK)
return JsonConvert.DeserializeObject<ApiResponse>(response.Content);
else
return new ApiResponse()
{
Status = false,
Result = null,
Message = response.ErrorMessage
};
}
public async Task<ApiResponse<T>> ExecuteAsync<T>(BaseRequest baseRequest)
{
//uri
var uri = new Uri(apiUrl + baseRequest.Route);
//new restrequest
//add uri,method
var request = new RestRequest(uri, baseRequest.Method);
//add Parameter
if (baseRequest.Parameter != null)
request.AddParameter("application/json", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
//判断client是否为空
if (client == null)
return new ApiResponse<T>()
{
Status = false,
Message = "client为空"
};
//执行
var response = await client.ExecuteAsync(request);
//如果成功则执行序列化任务,否则返回失败代码
if (response.StatusCode == System.Net.HttpStatusCode.OK)
return JsonConvert.DeserializeObject<ApiResponse<T>>(response.Content);
else
return new ApiResponse<T>()
{
Status = false,
Message = response.ErrorMessage
};
}