这是我的错误
SocketException:无法建立连接,因为目标机器主动拒绝。
我通过这个代码得到它
public class AppService : IAppService
{
public async Task<MainResponse> AuthenticateUser(LoginModel loginModel)
{
var returnResponse = new MainResponse();
using (var client = new HttpClient())
{
var url = $"{Setting.BaseUrl}{UserAPIs.AuthenticateUser}";
var serializedStr = JsonConvert.SerializeObject(loginModel);
var response = await client.PostAsync(url, new StringContent(serializedStr, Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
string contentStr = await response.Content.ReadAsStringAsync();
returnResponse = JsonConvert.DeserializeObject<MainResponse>(contentStr);
}
}
return returnResponse;
}
}
我需要程序来登录用户并显示索引页面
试试这些..
确保 API 已启动。
检查网址
在代码中添加异常处理
公共异步任务AuthenticateUser(LoginModel loginModel) { var returnResponse = new MainResponse();
try
{
using (var client = new HttpClient())
{
var url = $"{Setting.BaseUrl}{UserAPIs.AuthenticateUser}";
var serializedStr = JsonConvert.SerializeObject(loginModel);
var response = await client.PostAsync(url, new StringContent(serializedStr, Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
string contentStr = await response.Content.ReadAsStringAsync();
returnResponse = JsonConvert.DeserializeObject<MainResponse>(contentStr);
}
}
}
catch (Exception ex)
{
// Log or display the exception details for troubleshooting.
Console.WriteLine($"Error: {ex.Message}");
// You can also throw the exception if needed.
// throw;
}
return returnResponse;
}