//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; } }
private OrderSimInputCollection FillCreateOrderSimulateData() { // Fill Order Header. // Fill Order Item. // Fill Order Partner. Create Partner by Partner Role, Ship To and SoldTo. // Keep Order Schedulein Empty. // Keep Extension empty. // Keep Message Table empty. // Keep Order Card empty. // Keep orderCcardEx empty. // Keep orderCfgsBlob empty // Keep orderCfgsInst empty // Keep orderCfgsPartOf empty // Keep orderCfgsRef empty // Keep orderCfgsValue empty // Keep orderConditionEx empty // Keep orderIncomplete empty // Keep partneraddresses empty OrderSimInputCollection collection = new OrderSimInputCollection(); // Fill Order Simulate Data. OrderSimInput orderSimulate = new OrderSimInput(); // Bind OrderHead // Sales Org orderSimulate.SalesOrg = "NL68"; // Dist Chain orderSimulate.DistrChan = "01"; //Division orderSimulate.Division = "00"; //Complete DLV orderSimulate.ComplDlv = true; // Sales Document Type orderSimulate.DocType = "OR"; // Fill Head2ItemNav // Do foreach loop for the Order Iteam orderSimulate.Head2ItemsNav = new List <OrderSimInOrderItems>(); // Generate ItemSimulate OrderSimInOrderItems itemSimulate = new OrderSimInOrderItems() { // Bind Meta Data //NetValue = "000000000000000", //QtyReqDt ="0000000000000", //DlvDate ="", NetValue1 = "0.0000", ItmNumber = "000010", Material = "760406*",// 760226* ReqQty = "0000000000100", SalesUnit = "CON" }; orderSimulate.Head2ItemsNav.Add(itemSimulate); // Fill Items2CondNav orderSimulate.Items2CondNav = new List <OrderSimInOrderCond>(); OrderSimInOrderCond itemCondNav = new OrderSimInOrderCond() { // Bind Meta Data ItmNumber = "000000", CondType = "", CondValue = "0.000000000", Condvalue = "0.000000000" }; orderSimulate.Items2CondNav.Add(itemCondNav); //Fill Item2ShNav orderSimulate.Items2ShNav = new List <OrderSimInOrderSh>(); OrderSimInOrderSh item2shNav = new OrderSimInOrderSh() { // Bind Meta Data ItmNumber = "000000", SchedLine = "0000", ReqDate = "", ReqQty = "0.000", ConfirQty = "0.000" }; orderSimulate.Items2ShNav.Add(item2shNav); // Fill Head2MsgNav orderSimulate.Head2MsgNav = new List <OrderSimInMessages>(); OrderSimInMessages head2MsgNav = new OrderSimInMessages() { // Bind Meta Data Type = "", Code = "", Message = "", LogNo = "", LogMsgNo = "000000", MessageV1 = "", MessageV2 = "", MessageV3 = "", MessageV4 = "" }; orderSimulate.Head2MsgNav.Add(head2MsgNav); // Fill Head2CustNav orderSimulate.Head2CustNav = new List <OrderSimInOrderPartners>(); // There are 2 Cust Nav . //Sold To - SP OrderSimInOrderPartners head2CustNavSP = new OrderSimInOrderPartners() { // Bind Sold To - SP PartnRole = "AG", //"SP", PartnNumb = "155580" }; orderSimulate.Head2CustNav.Add(head2CustNavSP); // Fil the Ship to - SH OrderSimInOrderPartners head2CustNavSH = new OrderSimInOrderPartners() { // Bind Ship to - SH PartnRole = "WE",// "SH", PartnNumb = "1286277" }; orderSimulate.Head2CustNav.Add(head2CustNavSH); // Add OrderSimulate to OrderSimulate Object collection.d = orderSimulate; //var jsonData = JsonConvert.SerializeObject(collection, settings); return(collection); }
// 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); }