1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| static readonly HttpClient Client = new HttpClient(); public async Task<string> GetAsync(string url, object data) { try { string requestUrl = $"{url}?{GetQueryString(data)}"; logger.Info($"GetAsync Start, requestUrl:{requestUrl}"); var response = await Client.GetAsync(requestUrl).ConfigureAwait(false); string result = await response.Content.ReadAsStringAsync(); logger.Info($"GetAsync End, requestUrl:{requestUrl}, HttpStatusCode:{response.StatusCode}, result:{result}"); return result; } catch (WebException ex) { if (ex.Response != null) { string responseContent = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); throw new Exception($"Response :{responseContent}", ex); } throw; } }
private static string GetQueryString(object obj) { var properties = from p in obj.GetType().GetProperties() where p.GetValue(obj, null) != null select p.Name + "=" + HttpUtility.UrlEncode(p.GetValue(obj, null).ToString());
return String.Join("&", properties.ToArray()); }
|