问题描述
我有一个类,我用它来创建 HTTP 客户端的实例,以对任何所需的 API 执行 Get 操作。现在,当我在装有 Windows 7 的机器上使用它时,它在 Windows 10 中运行顺畅,它给我一条通用消息“发送请求时发生错误”。我是跳过了流程中的任何步骤还是兼容性/网络问题?
这是我的类代码
public static class HttpService
{
private static readonly HttpClient _httpClient;
static HttpService()
{
_httpClient = new HttpClient();
}
public static async Task<string> ExecuteGetStringAsync(string endpoint)
{
string res = string.Empty;
HttpResponseMessage response = await _httpClient.GetAsync(endpoint);
// Handle the response
switch (response.StatusCode)
{
case HttpStatusCode.OK:
res = await response.Content.ReadAsstringAsync();
_httpClient.DefaultRequestHeaders.ConnectionClose = true;
break;
case HttpStatusCode.NotFound:
await _logger.Error($"StatusCode: {HttpStatusCode.NotFound},Response: {res}");
throw new HttpException(404,"Not Found");
case HttpStatusCode.InternalServerError:
res = await response.Content.ReadAsstringAsync();
await _logger.Error($"StatusCode: {HttpStatusCode.InternalServerError},Response: {res}");
throw new HttpException(501,$"ExecuteGet->Failed: {res}");
case HttpStatusCode.Unauthorized:
await _logger.Error($"StatusCode: {HttpStatusCode.Unauthorized},Response: {res}");
throw new HttpException(401,"Unauthorized access");
default:
int statusCode = (int)response.StatusCode;
await _logger.Error($"StatusCode: {statusCode},Response: {res}");
throw new HttpException(statusCode,response.ReasonPhrase);
}
return res;
}
}
}
我正在使用该方法使用我使用 serverIp 构建的 API 连接服务器
private static async Task ConnectToServer(string serverIp)
{
string releaseUrl = string.Format("https://{0}/bin/APILinks?cmd=connectToServer",serverIp);
try
{
await HttpService.ExecuteGetStringAsync(releaseUrl);
}
catch (Exception ex)
{
await _logger.Error($",serverIp: {serverIp},cookie: {cookie},error: {ex.Message},stacktrace: {ex.StackTrace}");
}
}
error: An error occurred while sending the request.,stacktrace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at MyApplication.Common.HttpService.<ExecuteGetStringAsync>d__2.MoveNext()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at MyApplication.Common.Service.<ConnectToServer>d__38.MoveNext()\r\n--- End of stack trace from prevIoUs location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at MyApplication.Common.Service.<ConnectToServer>d__34.MoveNext()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)