ASP.NET Core Web API - 如何使用 HttpClient 使用第三方 API

ASP.NET Core Web API - How to Consume 3rd party API using HttpClient

在我的 ASP.NET Core-6 Web API 中,我被提供给第三方 API 使用,然后 return 帐户详细信息。

api:

https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber=112123412

Headers:

X-GivenID:Given2211
X-GivenName:Givenyou
X-GivenPassword:Given@llcool

然后JSON结果如下图:

{
  "AccountName": "string",
  "CurrentBalance": 0,
  "AvailableBalance": 0,
  "Currency": "string"
}

到目前为止,我已经这样做了:

余额查询响应:

public class BalanceEnquiryResponse
{
    public string Response
    {
        get;
        set;
    }

    public bool IsSuccessful
    {
        get;
        set;
    }

    public List<BalanceList> AccountBalances
    {
        get;
        set;
    }
}

余额列表:

public class BalanceList
{
    public string AccountNumber
    {
        get;
        set;
    }

    public decimal CurrentBalance
    {
        get;
        set;
    }

    public decimal AvailableBalance
    {
        get;
        set;
    }

    public string Currency
    {
        get;
        set;
    }
}

那么服务如下图

IDataService:

public interface IDataService
{
    BalanceEnquiryResponse GetAccountBalance(string accountNo);
}

数据服务:

public class DataService : IDataService
{
    private readonly ILogger<DataService> _logger;
    private readonly HttpClient _myClient;
    public DataService(ILogger<DataService> logger, HttpClient myClient)
    {
        _logger = logger;
        _myClient = myClient;
    }

    private void PrepareAPIHeaders()
    {
        _myClient.DefaultRequestHeaders.Add("X-GivenID", "Given2211");
        _myClient.DefaultRequestHeaders.Add("X-GivenName", "Givenyou");
        _myClient.DefaultRequestHeaders.Add("X-GivenPassword", "Given@llcool");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json; charset=utf-8");
    }

    public BalanceEnquiryResponse GetAccountBalance(string accountNo)
    {
        _logger.LogInformation("Accessing Own Account");
        var url = $"https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber={accountNo}";
        var responseResults = new BalanceEnquiryResponse();
        var response = await _myClient.GetAsync(url);
        return response;
    }
}

使用 HttpClient,我想 return 响应与 url、headers 和 BalanceEnquiryResponse

我第一次尝试使用 HttpClient 使用第三方 API,我正在关注这个 Consume Web API in .NET using HttpClient

到目前为止,我得到了这个错误:

Cannot implicitly convert type 'System.Net.Http.HttpResponseMessage' to 'BalanceEnquiryResponse'

响应return响应

中突出显示

如何更正错误并实现我在 return 响应中的目标。

谢谢。

根据您的 class,这些小改动应该可以让您解决其余问题:

public class DataService : IDataService
{
    private readonly ILogger<DataService> _logger;
    private readonly HttpClient _myClient;
    public DataService(ILogger<DataService> logger, HttpClient myClient)
    {
        _logger = logger;
        _myClient = myClient;
        PrepareAPIHeaders(); // Actually apply the headers!
    }

    private void PrepareAPIHeaders()
    {
        _myClient.DefaultRequestHeaders.Add("X-GivenID", "Given2211");
        _myClient.DefaultRequestHeaders.Add("X-GivenName", "Givenyou");
        _myClient.DefaultRequestHeaders.Add("X-GivenPassword", "Given@llcool");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json; charset=utf-8");
    }

    // If you want to use async API, you need to go async all the way.
    // So make this Method async, too!
    public async Task<BalanceEnquiryResponse> GetAccountBalance(string accountNo)
    {
        _logger.LogInformation("Accessing Own Account");
        var url = $"https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber={accountNo}";

        var response = await _myClient.GetAsync(url);
        // vv Get your payload out of the Http Response.
        var responseResults = await response.Content.ReadAsAsync<BalanceEnquiryResponse>();
        return responseResults;
    }
}