/// <summary> /// Post values to service and return reference type /// </summary> /// <typeparam name="T1"> Return object type of service</typeparam> /// <typeparam name="T2"> Input object type of service</typeparam> /// <param name="method"> Name of the rest method</param> /// <param name="t2"> Object needs to be posted to service</param> /// <returns></returns> public T1 PostData <T1, T2>(string method, T2 t2) where T1 : class { using (var proxy = new BKICWebClient()) { try { SetRequestHeaders(proxy); var serializedObject = JsonConvert.SerializeObject(t2); var mediaType = ""; HttpContent postContent; if (IsLoginAction) { var tokenSecurityInfo = t2 as BKIC.SellingPoint.DTO.RequestResponseWrappers.OAuthRequest; var login = new Dictionary <string, string> { { "grant_type", "password" }, { "username", tokenSecurityInfo.UserName }, { "password", tokenSecurityInfo.Password }, }; postContent = new FormUrlEncodedContent(login); } else { mediaType = "application/json"; postContent = new StringContent(serializedObject.ToString(), System.Text.Encoding.UTF8, mediaType); } //mediaType = "application/json"; //postContent = new StringContent(serializedObject.ToString(), System.Text.Encoding.UTF8, mediaType); var proxyPost = proxy.PostAsync(new Uri(WebApiPath + method), postContent); proxyPost.Wait(); HttpResponseMessage result = proxyPost.Result; if (result.IsSuccessStatusCode) { var response = JsonConvert.DeserializeObject <T1>(result.Content.ReadAsStringAsync().Result); return(response ?? (T1)Activator.CreateInstance(typeof(T1))); } else { return(BindBadRequestDetails <T1>(result)); } } catch (Exception exc) { return(BindRequestExceptionDetails <T1>(exc)); } } }
public T1 PostDataWithNormalDate <T1, T2>(string method, T2 t2) where T1 : class { using (var proxy = new BKICWebClient()) { try { SetRequestHeaders(proxy); var serializedObject = JsonConvert.SerializeObject(t2, new IsoDateTimeConverter() { DateTimeFormat = "dd/MM/yyyy" }); var mediaType = ""; HttpContent postContent; mediaType = "application/json"; postContent = new StringContent(serializedObject.ToString(), System.Text.Encoding.UTF8, mediaType); var proxyPost = proxy.PostAsync(new Uri(WebApiPath + method), postContent); proxyPost.Wait(); HttpResponseMessage result = proxyPost.Result; if (result.IsSuccessStatusCode) { var response = JsonConvert.DeserializeObject <T1>(result.Content.ReadAsStringAsync().Result, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); return(response ?? (T1)Activator.CreateInstance(typeof(T1))); } else { return(BindBadRequestDetails <T1>(result)); } } catch (Exception exc) { return(BindRequestExceptionDetails <T1>(exc)); } } }