如何解决我使用的 HTTP 库是否与 Windows 7 不兼容?
我有一个类,我用它来创建 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 举报,一经查实,本站将立刻删除。