using NetLibrary; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Script.Serialization; using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using System.Net; using System.IO; using System.Xml.Linq; namespace NetLibrary.OnlineTrade { public class CDiscountAPI { JavaScriptSerializer JsonConvert = null; public CDiscountAPI() { //验证服务器证书回调自动验证 ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate; JsonConvert = new JavaScriptSerializer(); } private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } public List GetOrders(string Username,string password,string xml, out string ErrorMessage) { ErrorMessage = ""; List ListModel = new List(); string str = xml.Replace("@Token@", GetTokenInteg2(Username, password)); string XmlContent = PostSend2(str); XmlContent = XmlContent.Replace("(); foreach (XElement item2 in item.Element("OrderLineList").Elements("OrderLine")) { OrderDetailModel model2 = new OrderDetailModel(); model2.GoodsNum = Convert.ToInt32(item2.Element("Quantity").Value); model2.GoodsPrice = Convert.ToDecimal(item2.Element("PurchasePrice").Value); totalprice += Convert.ToDecimal(item2.Element("PurchasePrice").Value); model2.MoneyCode = model.MoneyCode; model2.GoodsName = item2.Element("ProductCondition").Value; model2.GoodsSKU = item2.Element("SellerProductId").Value; if (item2.Element("ProductId").Value == "INTERETBCA") continue; model.ListModel.Add(model2); } model.TotalPrice = totalprice - Convert.ToDecimal(item.Element("SiteCommissionValidatedAmount").Value) + Convert.ToDecimal(item.Element("ValidatedTotalShippingCharges").Value); ListModel.Add(model); } } //string ordertemp=root.Element return ListModel; } public string SendTrack(string Username,string password,string xml) { string str = xml.Replace("@Token@", GetTokenInteg2(Username, password)); string XmlContent = PostSend3(str); return XmlContent; } public string PostSend3(string output) { string strReturn = ""; byte[] obtPostData = Encoding.UTF8.GetBytes(output.ToString()); ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; var webRequest = (HttpWebRequest)WebRequest.Create("https://wsvc.cdiscount.com/MarketplaceAPIService.svc"); webRequest.Headers.Add("SOAPAction", String.Format("\"{0}\"", "http://www.cdiscount.com/IMarketplaceAPIService/ValidateOrderList")); webRequest.ContentType = "text/xml;charset=\"utf-8\""; webRequest.Accept = "text/xml;charset=UTF-8"; webRequest.Method = "POST"; //orqRequest.Headers.Add("Authorization", string.Format("Basic={0}", encoded)); Stream stream; using (stream = webRequest.GetRequestStream()) { stream.Write(obtPostData, 0, obtPostData.Length); } HttpWebResponse orsResponse = (HttpWebResponse)webRequest.GetResponse(); using (Stream responseStream = orsResponse.GetResponseStream()) { using (StreamReader streamRead = new StreamReader(responseStream, Encoding.UTF8)) { strReturn = streamRead.ReadToEnd(); } } return strReturn; } public string PostSend2(string output) { string strReturn = ""; byte[] obtPostData = Encoding.UTF8.GetBytes(output.ToString()); ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; var webRequest = (HttpWebRequest)WebRequest.Create("https://wsvc.cdiscount.com/MarketplaceAPIService.svc"); webRequest.Headers.Add("SOAPAction", String.Format("\"{0}\"", "http://www.cdiscount.com/IMarketplaceAPIService/GetOrderList")); webRequest.ContentType = "text/xml;charset=\"utf-8\""; webRequest.Accept = "text/xml;charset=UTF-8"; webRequest.Method = "POST"; //orqRequest.Headers.Add("Authorization", string.Format("Basic={0}", encoded)); Stream stream; using (stream = webRequest.GetRequestStream()) { stream.Write(obtPostData, 0, obtPostData.Length); } HttpWebResponse orsResponse = (HttpWebResponse)webRequest.GetResponse(); using (Stream responseStream = orsResponse.GetResponseStream()) { using (StreamReader streamRead = new StreamReader(responseStream, Encoding.UTF8)) { strReturn = streamRead.ReadToEnd(); } } return strReturn; } public static string GetTokenInteg2(string username, string password) { const string svcIssue = "https://sts.cdiscount.com/users/httpIssue.svc"; const string svcToCall = "https://wsvc.cdiscount.com/MarketplaceAPIService.svc"; string strReturn = ""; ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; string encoded = Convert.ToBase64String(Encoding.Default.GetBytes(string.Format("{0}:{1}", username, password))); var stsUri = new Uri(string.Format("{0}/?realm={1}", svcIssue, svcToCall)); HttpWebRequest orqRequest = (HttpWebRequest)WebRequest.Create(stsUri); orqRequest.Timeout = 3000; orqRequest.Method = "GET"; orqRequest.ContentType = "text/xml; charset=utf-8"; orqRequest.Headers.Add("Authorization", string.Format("Basic={0}", encoded)); HttpWebResponse orsResponse = (HttpWebResponse)orqRequest.GetResponse(); using (Stream responseStream = orsResponse.GetResponseStream()) { using (StreamReader streamRead = new StreamReader(responseStream, Encoding.UTF8)) { strReturn = streamRead.ReadToEnd(); } } string[] tokenlist = strReturn.Split('>'); string[] list2 = tokenlist[1].Split('<'); return list2[0]; } } public class CDOrder { public string ArchiveParcelList { get; set; } public string order_id { get; set; } //public CDOrderShippingAddress ShippingAddress { get; set; } public string CreationDate { get; set; } public CDOrderCustomer Customer { get; set; } public string HasClaims { get; set; } public string InitialTotalAmount { get; set; } public string InitialTotalShippingChargesAmount { get; set; } public string LastUpdatedDate { get; set; } public string ModifiedDate { get; set; } public string Offer { get; set; } public List OrderLineList { get; set; } public string OrderNumber { get; set; } public string OrderState { get; set; } public string ShippedTotalAmount { get; set; } public string ShippedTotalShippingCharges { get; set; } public CDOrderShippingAddress ShippingAddress { get; set; } public string ShippingCode { get; set; } public string SiteCommissionPromisedAmount { get; set; } public string SiteCommissionShippedAmount { get; set; } public string SiteCommissionValidatedAmount { get; set; } public string Status { get; set; } public string ValidatedTotalAmount { get; set; } public string ValidatedTotalShippingCharges { get; set; } public string ValidationStatus { get; set; } public CDOrderCorporation Corporation { get; set; } } public class CDOrderBillAddress { public string Address1 { get; set; } public string Address2 { get; set; } public string ApartmentNumber { get; set; } public string Building { get; set; } public string City { get; set; } public string Civility { get; set; } public string CompanyName { get; set; } public string Country { get; set; } public string FirstName { get; set; } public string Instructions { get; set; } public string LastName { get; set; } public string PlaceName { get; set; } public string Street { get; set; } public string ZipCode { get; set; } } public class CDOrderCustomer { public string Civility { get; set; } public string CustomerId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string MobilePhone { get; set; } public string Phone { get; set; } } public class CDOrderGood { public CDOrderGoodDetail OrderLine { get; set; } } public class CDOrderGoodDetail { public string AcceptationState { get; set; } public string CategoryCode { get; set; } public string DeliveryDateMax { get; set; } public string DeliveryDateMin { get; set; } public string HasClaim { get; set; } public string IsCDAV { get; set; } public string Name { get; set; } public string OrderLineChildList { get; set; } public string ProductCondition { get; set; } public string ProductEan { get; set; } public string ProductId { get; set; } public string PurchasePrice { get; set; } public string Quantity { get; set; } public string RowId { get; set; } public string SellerProductId { get; set; } public string ShippingDateMax { get; set; } public string ShippingDateMin { get; set; } public string Sku { get; set; } public string SkuParent { get; set; } public string UnitAdditionalShippingCharges { get; set; } public string UnitShippingCharges { get; set; } } public class CDOrderShippingAddress { public string Address1 { get; set; } public string Address2 { get; set; } public string ApartmentNumber { get; set; } public string Building { get; set; } public string City { get; set; } public string Civility { get; set; } public string CompanyName { get; set; } public string Country { get; set; } public string FirstName { get; set; } public string Instructions { get; set; } public string LastName { get; set; } public string PlaceName { get; set; } public string Street { get; set; } public string ZipCode { get; set; } } public class CDOrderCorporation { public string CorporationId { get; set; } public string CorporationName { get; set; } public string BusinessUnitId { get; set; } public string IsMarketPlaceActive { get; set; } public string CorporationCode { get; set; } } }