gpt4 book ai didi

c# - 在 winform C# 应用程序中调用 Web api 并获取响应

转载 作者:行者123 更新时间:2023-11-30 21:28:26 28 4
gpt4 key购买 nike

我正在 Windows 中制作一个简单的 WinForm 应用程序,我想获取一些有关外汇汇率的数据。所以我决定调用 Oanda 的 API。我尝试了几件事,但没有任何效果。它以 CSV 和 JSON 格式给出响应。我不知道哪个会更容易处理。

对于这种类型的响应,我无法创建其模型类。回应:

JSON:

{
"meta": {
"effective_params": {
"data_set": "OANDA",
"base_currencies": [
"EUR"
],
"quote_currencies": [
"USD"
]
},
"endpoint": "spot",
"request_time": "2019-06-08T12:05:23+00:00",
"skipped_currency_pairs": []
},
"quotes": [
{
"base_currency": "EUR",
"quote_currency": "USD",
"bid": "1.13287",
"ask": "1.13384",
"midpoint": "1.13336"
}
]
}

CSV:

base_currency,quote_currency,bid,ask,midpoint
EUR,USD,1.13287,1.13384,1.13336

我只需要这三个数字,那么哪种方法会有帮助以及如何帮助。

我已经尝试过此代码:

var client = new HttpClient();
client.BaseAddress = new Uri("https://www1.oanda.com/rates/api/v2/rates/");
HttpResponseMessage response = await client.GetAsync("spot.csv?api_key=<myapikey>&base=EUR&quote=USD");
string result = await response.Content.ReadAsStringAsync();
textBox1.Text = result;

编辑:我需要此调用的结果进行进一步处理,因此我必须需要此方法来完成其执行,然后再继续进行

最佳答案

First creating model from Json:

  1. 使用在线模型生成器,例如 Json2C# ,对于您发布的 Json,以下是生成的模型:

    public class EffectiveParams
    {
    public string data_set { get; set; }
    public List<string> base_currencies { get; set; }
    public List<string> quote_currencies { get; set; }
    }

    public class Meta
    {
    public EffectiveParams effective_params { get; set; }
    public string endpoint { get; set; }
    public DateTime request_time { get; set; }
    public List<object> skipped_currency_pairs { get; set; }
    }

    public class Quote
    {
    public string base_currency { get; set; }
    public string quote_currency { get; set; }
    public string bid { get; set; }
    public string ask { get; set; }
    public string midpoint { get; set; }
    }

    public class RootObject
    {
    public Meta meta { get; set; }
    public List<Quote> quotes { get; set; }
    }
  2. 现在使用 HttpClient 连接到 WebAPI,它可以选择返回 JsonCSV,我希望 JSON 成为标准,也可以被各种客户端轻松使用,请使用以下简单的通用方法:

Assuming it is GET only call, just supply the Host and API details to the generic Process method underneath:

public async Task<TResponse> Process<TResponse>(string host,string api)
{
// Execute Api call Async
var httpResponseMessage = await MakeApiCall(host,api);

// Process Json string result to fetch final deserialized model
return await FetchResult<TResponse>(httpResponseMessage);
}

public async Task<HttpResponseMessage> MakeApiCall(string host,string api)

{
// Create HttpClient
var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }) { BaseAddress = new Uri(host) };
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

// Make an API call and receive HttpResponseMessage
HttpResponseMessage responseMessage = await client.GetAsync(api, HttpCompletionOption.ResponseContentRead);

return responseMessage;
}

public async Task<T> FetchResult<T>(HttpResponseMessage result)
{
if (result.IsSuccessStatusCode)
{
// Convert the HttpResponseMessage to string
var resultArray = await result.Content.ReadAsStringAsync();

// Json.Net Deserialization
var final = JsonConvert.DeserializeObject<T>(resultArray);

return final;
}
return default(T);
}

How to use:

  • 只需调用:

    var rootObj = await Process<RootObject>("https://www1.oanda.com/rates/", "api/v2/rates/");
  • 您会收到反序列化的 RootObject,如上面的模型所示

  • 对于任何进一步复杂的处理,例如使用 http 正文将输入发送到调用,上面的通用代码需要进一步修改,它目前仅特定于您的要求

编辑1:(使条目调用同步)

  • 要使整体调用同步,请在最顶层使用GetAwaiter().GetResult(),Main方法将被转换为,其余的将与示例中的保持一致(异步方法)

      void Main()
    {
    var rootObj = Process<RootObject>("https://www1.oanda.com/rates/", "api/v2/rates/").GetAwaiter().GetResult();
    }

编辑2:(使完整代码同步)

void Main()
{
var rootObj = Process<RootObject>("https://www1.oanda.com/rates/", "api/v2/rates/");
}


public TResponse Process<TResponse>(string host, string api)
{
// Execute Api call
var httpResponseMessage = MakeApiCall(host, api);

// Process Json string result to fetch final deserialized model
return FetchResult<TResponse>(httpResponseMessage);
}

public HttpResponseMessage MakeApiCall(string host, string api)

{
// Create HttpClient
var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }) { BaseAddress = new Uri(host) };
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

// Make an API call and receive HttpResponseMessage
HttpResponseMessage responseMessage = client.GetAsync(api, HttpCompletionOption.ResponseContentRead).GetAwaiter().GetResult();

return responseMessage;
}

public T FetchResult<T>(HttpResponseMessage result)
{
if (result.IsSuccessStatusCode)
{
// Convert the HttpResponseMessage to string
var resultArray = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();

// Json.Net Deserialization
var final = JsonConvert.DeserializeObject<T>(resultArray);

return final;
}
return default(T);
}

关于c# - 在 winform C# 应用程序中调用 Web api 并获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56506317/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com