/// <summary> /// Send Http GET request /// </summary> /// <typeparam name="T">The response type</typeparam> /// <param name="url">Request url</param> /// <returns>The generic response type</returns> public static async Task <T> GetAsync <T>(string url) { var httpClient = new HttpClient(); _logger.RquestStart("GET", url); string response = await httpClient.GetStringAsync(url); _logger.RequestEnd(); T t = JsonConvert.DeserializeObject <T>(response); return(t); }
/// <summary> /// Send Http GET request /// </summary> /// <typeparam name="T">The response type</typeparam> /// <param name="url">Request url</param> /// <returns>The generic response type</returns> public static async Task <T> GetAsync <T>(string url) { try { _logger.RquestStart("GET", url); string response = await httpClient.GetStringAsync(url); _logger.RequestEnd(); T t = JsonConvert.DeserializeObject <T>(response); return(t); } catch (Exception e) { UnityEngine.Debug.LogError(e); throw; } }
public static async Task <T> Get <T>(string apiUrl, IDictionary <string, string> paras, bool isSign = false, string apiKey = "", string apiSecret = "", string host = "", CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(apiUrl)) { throw new ArgumentNullException(nameof(apiUrl)); } using (var request = new HttpRequestMessage(HttpMethod.Get, QueryHelpers.AddQueryString(apiUrl, AddCommonParas(paras)))) { BuildHead(request, GET_METHOD, apiUrl, JsonConvert.SerializeObject(null), isSign, apiKey, apiSecret); if (!string.IsNullOrEmpty(host)) { _client.BaseAddress = new Uri(host); } _logger.RquestStart(GET_METHOD, apiUrl); using (var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false)) { _logger.RequestEnd(response.StatusCode.ToString()); response.EnsureSuccessStatusCode(); if (response.StatusCode == System.Net.HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(await response.Content .ReadAsStreamAsync().ConfigureAwait(false))) using (JsonReader reader = new JsonTextReader(sr)) { var serializer = JsonSerializer.CreateDefault(); var rps = serializer.Deserialize <ApiResponseModel <T> >(reader); if (!string.IsNullOrEmpty(rps.Message)) { throw new InvokerFailException(rps.Message); } if (rps.Ret != 0) { throw new InvokerFailException(rps.ErrCode, rps.ErrStr); } return(rps.Data); } } return(default);