private static void Upload(string mainProductId, SubProduct subProduct) { var uploadUrl = ConfigurationManager.AppSettings["SubUploadURL"]; uploadUrl = uploadUrl.Replace("[MAINPRODUCTID]", mainProductId); var httpWebReq = (HttpWebRequest)WebRequest.Create(uploadUrl); var encoding = new UTF8Encoding(); var postDataString = subProduct.ToString(); var data = encoding.GetBytes(postDataString); httpWebReq.ContentLength = Encoding.UTF8.GetByteCount(postDataString); httpWebReq.Accept = ConfigurationManager.AppSettings["Http_Accept"]; httpWebReq.UserAgent = ConfigurationManager.AppSettings["Http_UserAgent"]; httpWebReq.ContentType = ConfigurationManager.AppSettings["Http_ContentType"]; var httpReferer = ConfigurationManager.AppSettings["Http_Referer_SubProduct"]; httpReferer = httpReferer.Replace("[MAINPRODUCTID]", mainProductId); httpWebReq.Referer = httpReferer; httpWebReq.Headers["Accept-Encoding"] = ConfigurationManager.AppSettings["Http_AcceptEncoding"]; httpWebReq.Headers["Accept-Language"] = ConfigurationManager.AppSettings["Http_AcceptLanguage"]; httpWebReq.Headers["Cookie"] = ConfigurationManager.AppSettings["Http_Cookie"]; httpWebReq.Method = "POST"; using (var stream = httpWebReq.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = (HttpWebResponse)httpWebReq.GetResponse(); if (response == null) { throw new Exception("Upload to PAYEAR.COM is failed"); } var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); var jObject = JObject.Parse(responseString); var responseMessage = (bool)jObject["success"]; if (responseMessage == false) { throw new Exception("Upload to PAYEAR.COM is failed"); } var resultHtml = (string)jObject["data"]["table_tr"]; var doc = new HtmlDocument(); doc.LoadHtml(resultHtml); HtmlNode resultNode = doc.DocumentNode.SelectSingleNode("/tr"); var subProductId = resultNode.Attributes["proid"].Value; // Set Quantity SetQuantity(mainProductId, subProductId, subProduct.Quantity); }
private static void Upload(string mainProductId, SubProduct subProduct) { var uploadUrl = ConfigurationManager.AppSettings["SubUploadURL"]; uploadUrl = uploadUrl.Replace("[MAINPRODUCTID]", mainProductId); var httpWebReq = (HttpWebRequest)WebRequest.Create(uploadUrl); var encoding = new UTF8Encoding(); var postDataString = subProduct.ToString(); var data = encoding.GetBytes(postDataString); httpWebReq.ContentLength = Encoding.UTF8.GetByteCount(postDataString); httpWebReq.Accept = ConfigurationManager.AppSettings["Http_Accept"]; httpWebReq.UserAgent = ConfigurationManager.AppSettings["Http_UserAgent"]; httpWebReq.ContentType = ConfigurationManager.AppSettings["Http_ContentType"]; var httpReferer = ConfigurationManager.AppSettings["Http_Referer_SubProduct"]; httpReferer = httpReferer.Replace("[MAINPRODUCTID]", mainProductId); httpWebReq.Referer = httpReferer; httpWebReq.Headers["Accept-Encoding"] = ConfigurationManager.AppSettings["Http_AcceptEncoding"]; httpWebReq.Headers["Accept-Language"] = ConfigurationManager.AppSettings["Http_AcceptLanguage"]; httpWebReq.Headers["Cookie"] = ConfigurationManager.AppSettings["Http_Cookie"]; httpWebReq.Method = "POST"; using (var stream = httpWebReq.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = (HttpWebResponse)httpWebReq.GetResponse(); if (response == null) throw new Exception("Upload to PAYEAR.COM is failed"); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); var jObject = JObject.Parse(responseString); var responseMessage = (bool)jObject["success"]; if (responseMessage == false) throw new Exception("Upload to PAYEAR.COM is failed"); var resultHtml = (string)jObject["data"]["table_tr"]; var doc = new HtmlDocument(); doc.LoadHtml(resultHtml); HtmlNode resultNode = doc.DocumentNode.SelectSingleNode("/tr"); var subProductId = resultNode.Attributes["proid"].Value; // Set Quantity SetQuantity(mainProductId, subProductId, subProduct.Quantity); }