//This Method using Http_Client private Order OrderSimulateToSAP_HttpClient(OrderSimInputCollection collection) { Order orderSimulate = new Order(); // Get Cookies and csrfToken if (string.IsNullOrEmpty(csrfTokenOrderSimulate)) { GetCSRFTokenForSimulation(); } var cookieContainerOrderSimulate = new CookieContainer(); using (var handler = new HttpClientHandler { CookieContainer = cookieContainerOrderSimulate, Credentials = new NetworkCredential(sapUserName, sapPassword) }) using (var client = new HttpClient(handler)) { client.BaseAddress = new Uri(sapOrderSimulationPost); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Add("X-CSRF-Token", csrfTokenOrderSimulate); cookieContainerOrderSimulate.Add(client.BaseAddress, cookiestopassOrderSimulate); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var httpContent = new StringContent(JsonConvert.SerializeObject(collection, settings) , UnicodeEncoding.UTF8, "application/json"); var response = client.PostAsync(client.BaseAddress, httpContent).Result; if (response.StatusCode == HttpStatusCode.Created) { // Get response data var responseJsonData = response.Content.ReadAsStringAsync().Result; // Deserialize the Response JSON data to OrderSimCollection. OrderSimOutCollection responseCollection = JsonConvert.DeserializeObject <OrderSimOutCollection>(responseJsonData); } else if (response.StatusCode == HttpStatusCode.BadRequest) { // Log for the bad request. var message = response.Content.ReadAsStringAsync().Result; // Log Message } } return(orderSimulate); }
public void ProcessOrderSimulate() { OrderSimOutCollection orderSimOutCollection = new OrderSimOutCollection(); // Fill the Order collect OrderSimInputCollection collection = this.FillCreateOrderSimulateData(); try { // Push and get Order to SAP for Simulate orderSimOutCollection = OrderSimulateToSAP(collection); // todo : Now Map all Order Data as per the IQL Mapping } catch (Exception ex) { // Need to log the exception details. throw ex; } }
// This method using WebRequest private OrderSimOutCollection OrderSimulateToSAP(OrderSimInputCollection collection) { OrderSimOutCollection orderSimOutCollection = new OrderSimOutCollection(); try { // Get Cookies and csrfToken if (string.IsNullOrEmpty(csrfTokenOrderSimulate)) { GetCSRFTokenForSimulation(); } // Using the HTTPWebRequest. var cookieContainerOrderSimulate = new CookieContainer(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sapOrderSimulationPost); // Add Credentional, Header, Token & Cookies req.Credentials = new NetworkCredential(sapUserName, sapPassword); req.Method = "POST"; req.Headers.Add("X-CSRF-Token", csrfTokenOrderSimulate); req.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; req.Accept = "application/json"; req.ContentType = "application/json"; cookieContainerOrderSimulate.Add(new Uri(sapOrderSimulationPost), cookiestopassOrderSimulate); req.CookieContainer = cookieContainerOrderSimulate; // Get Order Simulate JSON Data from Entity var jsonData = JsonConvert.SerializeObject(collection, settings); // Enclding JSON Data UTF8Encoding encoding = new UTF8Encoding(); Byte[] bytes = encoding.GetBytes(jsonData); Stream newStream = req.GetRequestStream(); newStream.Write(bytes, 0, bytes.Length); newStream.Close(); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { // Get the response Content from Webresponse var rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd(); if (response.StatusCode == HttpStatusCode.Created) { // Deserialize the Response JSON data to Order data. OrderSimOutCollection responseCollection = JsonConvert.DeserializeObject <OrderSimOutCollection>(rawJson); if (responseCollection != null) { orderSimOutCollection = responseCollection; } } else if (response.StatusCode == HttpStatusCode.BadRequest) { // Log for the bad request. string errorMessage = rawJson; //Log.Info(errorMessage); } } } catch (WebException webex) { var exceptionJson = new StreamReader(webex.Response.GetResponseStream()).ReadToEnd(); // Deserialize the Response JSON data to Error Model ErrorMessage errorMessage = JsonConvert.DeserializeObject <ErrorMessage>(exceptionJson); // Log string message = errorMessage.error.message.value; //Log.Info(errorMessage); } catch (Exception ex) { // Log the message in case any exception //Log.Info(ex.Message); } return(orderSimOutCollection); }