private ApiJsonResultCrossRequest <T> ReadResponse(HttpResponseMessage responseMessage) { var result = new ApiJsonResultCrossRequest <T>(); if (responseMessage.StatusCode == HttpStatusCode.OK) { var dateTimeConverter = new IsoDateTimeConverter { DateTimeFormat = DATETIME_FORMAT }; string responseContent = responseMessage.Content.ReadAsStringAsync().Result; if (_responseFormatOption == ResponFormatOption.ApiJsonResult) { result = JsonConvert.DeserializeObject <ApiJsonResultCrossRequest <T> >(responseContent, dateTimeConverter); } else { var normalResult = JsonConvert.DeserializeObject <T>(responseContent, dateTimeConverter); result.Success = true; result.Data = normalResult; } } else { result.Success = false; result.ErrorMessage = responseMessage.ReasonPhrase; } return(result); }
/// <summary> /// Excute sending request to destination by <paramref name="url"/>. /// <paramref name="postData"/> is null if method option is GET /// </summary> /// <param name="methodOption"></param> /// <param name="url"></param> /// <param name="postData">null if method option is GET</param> /// <returns></returns> public async Task <ApiJsonResultCrossRequest <T> > DoRequest(MethodOption methodOption, string url, object postData = null, Dictionary <string, string> headerValues = null) { if (methodOption == MethodOption.Post && postData == null) { ApiJsonResultCrossRequest <T> exeptionResult = new ApiJsonResultCrossRequest <T>() { Success = false, ErrorMessage = "DATA_POST_NULL" }; return(exeptionResult); } try { var response = new HttpResponseMessage(); if (methodOption == MethodOption.Get) { response = await GetAsync(url); } else { if (_requestDataFormatOption == RequestDataFormatOption.MultiFormFormat) { var data = postData.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .ToDictionary(prop => prop.Name, prop => prop.GetValue(postData, null) != null ? prop.GetValue(postData).ToString() : ""); response = await PostWithMultiFormAsync(url, data, headerValues); } else { response = await PostAsync(url, postData, headerValues); } } var result = ReadResponse(response); return(result); } catch (Exception ex) { ApiJsonResultCrossRequest <T> exeptionResult = new ApiJsonResultCrossRequest <T>() { Success = false, ErrorMessage = ex.Message }; return(exeptionResult); } }