JuHeApiService.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using OASystem.Domain.ViewModels.JuHeExchangeRate;
  2. using System.Net.Http;
  3. using System.Net.Http.Json;
  4. using Ubiety.Dns.Core;
  5. namespace OASystem.API.OAMethodLib.JuHeAPI
  6. {
  7. /// <summary>
  8. /// 聚合Api 服务
  9. /// </summary>
  10. public class JuHeApiService: IJuHeApiService
  11. {
  12. private readonly HttpClient _httpClient;
  13. private readonly string _appKey = "0f5429e9fbb8637c0ff3f14bbb42c732"; //配置您申请的appkey
  14. /// <summary>
  15. /// 初始化
  16. /// </summary>
  17. /// <param name="httpClient"></param>
  18. public JuHeApiService(HttpClient httpClient)
  19. {
  20. _httpClient = httpClient;
  21. _httpClient.BaseAddress = new Uri("http://web.juhe.cn:8080");
  22. }
  23. /// <summary>
  24. /// 汇率转换
  25. /// </summary>
  26. /// <returns></returns>
  27. /// <exception cref="NotImplementedException"></exception>
  28. public async Task<string> GetExchangeRateAsync()
  29. {
  30. string url = string.Format("/finance/exchange/rmbquot");
  31. var parameters2 = new Dictionary<string, string>();
  32. parameters2.Add("key", _appKey);//你申请的key
  33. parameters2.Add("type", "0"); //两种格式(0或者1,默认为0)
  34. parameters2.Add("bank", "3"); //(0:工商银行,1:招商银行,2:建设银行,3:中国银行,4:交通银行,5:农业银行,默认为:0)
  35. var httpContent = new StringContent(JsonConvert.SerializeObject(parameters2), Encoding.UTF8, "application/json");
  36. var exchangeReq = await _httpClient.PostAsync(url, httpContent);
  37. if (exchangeReq.IsSuccessStatusCode)
  38. {
  39. var stringResponse = await exchangeReq.Content.ReadAsStringAsync();
  40. }
  41. else {
  42. }
  43. throw new HttpRequestException(exchangeReq.ReasonPhrase);
  44. }
  45. }
  46. }