using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Threading; namespace WebServiceTest { class Program { static void Main(string[] args) { GoodSync(); Thread.Sleep(1000); TypeSync(); Thread.Sleep(1000); StoreSync(); //GetByCode(); Console.ReadLine(); } private static void GoodSync() { //接口参数内容 object o_Data = new { timestamp = (long)(DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalMilliseconds, goodsCode = "00014953", goodsName = "回香斋八角粒30g", img = "", unit = "包", nowPrice = "2.00", sort = "0", extParam = new { } }; Console.WriteLine($"加密前的数据:{JsonConvert.SerializeObject(o_Data)}"); //发送的接口数据 object o_Temp = new { appid = "AK202209070148347162", operation = 0, data = AESEncrypt(JsonConvert.SerializeObject(o_Data), "0ae62dc278de2ada91808ac501e81171", "", true) }; string str_ServiceURL = $"http://10.103.1.8:17080/DataTransferService/Service.asmx"; string str_PostData = JsonConvert.SerializeObject(o_Temp); Console.WriteLine($"发送的数据:{str_PostData}"); Hashtable hashtable = new Hashtable { { "requestURL", "https://ms.cjene.cn/api/open/do/merchant/goodsSync" }, { "postData", str_PostData }, { "contentType", "application/json" } }; try { //调用接口获取交易结果 string retString = ESSupport.Lib.SoapWSHelper.QuerySoapWebServiceString(str_ServiceURL, "MobilePayProxy", hashtable); Console.WriteLine($"浙江商业集团会员门店同步接口调用成功。接口返回值:{retString}"); } catch (Exception ex) { Console.WriteLine($"浙江商业集团会员门店同步接口调用失败:{ex.Message}"); } } private static void TypeSync() { //接口参数内容 object o_Data = new { timestamp = (long)(DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalMilliseconds, classifyCode = "3112", classifyName = "扶贫产品", sort = "0", goodsCodeList= new List { "00014698", "00014953" } }; Console.WriteLine($"加密前的数据:{JsonConvert.SerializeObject(o_Data)}"); //发送的接口数据 object o_Temp = new { appid = "AK202209070148347162", operation = 0, data = AESEncrypt(JsonConvert.SerializeObject(o_Data), "0ae62dc278de2ada91808ac501e81171", "", true) }; string str_ServiceURL = $"http://10.103.1.8:17080/DataTransferService/Service.asmx"; string str_PostData = JsonConvert.SerializeObject(o_Temp); Console.WriteLine($"发送的数据:{str_PostData}"); Hashtable hashtable = new Hashtable { { "requestURL", "https://ms.cjene.cn/api/open/do/merchant/storeClassifySync" }, { "postData", str_PostData }, { "contentType", "application/json" } }; try { //调用接口获取交易结果 string retString = ESSupport.Lib.SoapWSHelper.QuerySoapWebServiceString(str_ServiceURL, "MobilePayProxy", hashtable); Console.WriteLine($"浙江商业集团会员门店同步接口调用成功。接口返回值:{retString}"); } catch (Exception ex) { Console.WriteLine($"浙江商业集团会员门店同步接口调用失败:{ex.Message}"); } } private static void StoreSync() { //接口参数内容 object o_Data = new { timestamp = (long)(DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalMilliseconds, storeCode = "3408", storeName = "驿商测试门店", logo = "", storeImg = "", serviceAreaChildCode = "100000009", merchantCode = "348888012001", storePositionCode = "348888012001", openType = 0, lng = "120.2112714424", lat = "30.3043283639", storeClassify = new List { "3112" }, extParam = new { } }; Console.WriteLine($"加密前的数据:{JsonConvert.SerializeObject(o_Data)}"); //发送的接口数据 object o_Temp = new { appid = "AK202209070148347162", operation = 0, data = AESEncrypt(JsonConvert.SerializeObject(o_Data), "0ae62dc278de2ada91808ac501e81171", "", true) }; string str_ServiceURL = $"http://10.103.1.8:17080/DataTransferService/Service.asmx"; string str_PostData = JsonConvert.SerializeObject(o_Temp); Console.WriteLine($"发送的数据:{str_PostData}"); Hashtable hashtable = new Hashtable { { "requestURL", "https://ms.cjene.cn/api/open/do/merchant/storeSync" }, { "postData", str_PostData }, { "contentType", "application/json" } }; try { //调用接口获取交易结果 string retString = ESSupport.Lib.SoapWSHelper.QuerySoapWebServiceString(str_ServiceURL, "MobilePayProxy", hashtable); Console.WriteLine($"浙江商业集团会员门店同步接口调用成功。接口返回值:{retString}"); } catch (Exception ex) { Console.WriteLine($"浙江商业集团会员门店同步接口调用失败:{ex.Message}"); } } private static void GetByCode() { DataTable goodsTable = new DataTable(); goodsTable.Columns.Add("commodity_barcode", typeof(string)); //商品条码 goodsTable.Columns.Add("commodity_name", typeof(string)); //商品名称 goodsTable.Columns.Add("count", typeof(decimal)); //销售数量 goodsTable.Columns.Add("commodity_retailprice", typeof(decimal)); //销售单价 goodsTable.Columns.Add("discount_rate", typeof(decimal)); //优惠金额 goodsTable.Columns.Add("price", typeof(decimal)); //应收金额 DataRow dataRow = goodsTable.NewRow(); dataRow["commodity_barcode"] = "123456789"; dataRow["commodity_name"] = "测试商品"; dataRow["count"] = 1; dataRow["commodity_retailprice"] = 2; dataRow["price"] = 2; goodsTable.Rows.Add(dataRow); //交易列表 List o_goodsList = new List(); for (int i = 0; i < goodsTable.Rows.Count; i++) { o_goodsList.Add(new { goodsCode = goodsTable.Rows[i]["Commodity_Barcode"].ToString(), goodsName = goodsTable.Rows[i]["Commodity_Name"].ToString(), number = goodsTable.Rows[i]["Count"].ToString(), singlePrice = goodsTable.Rows[i]["Commodity_RetailPrice"].ToString(), totalAmount = goodsTable.Rows[i]["Price"].ToString(), }); } //接口参数内容 object o_Data = new { timestamp = (long)(DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalMilliseconds, dynamicCode = "HYM202209131014420285", storeCode = "249", orderAmount = "2.0", orderno = "20220908113700", discountAmount = "0.0", goodsList = o_goodsList, extParam = new { } }; Console.WriteLine($"加密前的数据:{JsonConvert.SerializeObject(o_Data)}"); //发送的接口数据 object o_Temp = new { appid = "AK202209070148347162", data = AESEncrypt(JsonConvert.SerializeObject(o_Data), "0ae62dc278de2ada91808ac501e81171", "", true) }; string str_ServiceURL = $"http://10.103.1.8:17080/DataTransferService/Service.asmx"; string str_PostData = JsonConvert.SerializeObject(o_Temp); Console.WriteLine($"发送的数据:{str_PostData}"); Hashtable hashtable = new Hashtable { { "requestURL", "https://ms.cjene.cn/api/open/do/coupon/getByMemberCode" }, { "postData", str_PostData }, { "contentType", "application/json" } }; try { //调用接口获取交易结果 string retString = ESSupport.Lib.SoapWSHelper.QuerySoapWebServiceString(str_ServiceURL, "MobilePayProxy", hashtable); Console.WriteLine($"浙江商业集团会员券核销接口调用成功。接口返回值:{retString}"); } catch (Exception ex) { Console.WriteLine($"浙江商业集团会员券核销接口调用失败:{ex.Message}"); } } #region AES加密 /// /// AES加密 /// /// 明文 /// 密钥,长度为16的字符串 /// 偏移量,长度为16的字符串 /// /// True:输出Hex格式密文 /// False:输出Base64格式密文 /// /// 密文 public static string AESEncrypt(string text, string key, string iv = "", bool isHex = false) { if (key.Length > 16) { key = key.Substring(0,16); } //密码转换Byte数据 byte[] pwdBytes = Encoding.UTF8.GetBytes(key); //使用加密后的密钥对数据进行AES加密 AesManaged rijndaelCipher = new AesManaged { Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7, KeySize = 128, BlockSize = 128, Key = pwdBytes }; if (!string.IsNullOrWhiteSpace(iv)) { byte[] ivBytes = Encoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes;//需要IV的启用这两句 } ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(text); byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length); if (isHex) return ToHex(cipherBytes);//输出为hex即启用此句,注释上一句 else return Convert.ToBase64String(cipherBytes);//输出为Base64即启用此句,注释下一句 } #endregion #region AES解密 /// /// AES解密 /// /// 密文 /// 密钥,长度为16的字符串 /// 偏移量,长度为16的字符串 /// /// True:输入Hex格式密文 /// False:输入Base64格式密文 /// 明文 public static string AESDecrypt(string text, string key, string iv = "", bool isHex = false) { if (key.Length > 16) { key = key.Substring(0, 16); } byte[] pwdBytes = Encoding.UTF8.GetBytes(key); AesManaged rijndaelCipher = new AesManaged { Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7, KeySize = 128, BlockSize = 128, Key = pwdBytes }; if (!string.IsNullOrWhiteSpace(iv)) { byte[] ivBytes = Encoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes;//需要IV的启用这两句 } byte[] encryptedData; if (isHex) encryptedData = UnHex(text);//输出为hex即启用此句,注释上一句 else encryptedData = Convert.FromBase64String(text);//输出为Base64即启用此句,注释下一句 ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); } #endregion #region Hex与byte转码 /// /// 从字符串转换到16进制表示的字符串 /// /// 需要转码的byte /// 返回结果 private static string ToHex(byte[] bytes) { string str = string.Empty; if (bytes != null || bytes.Length > 0) { for (int i = 0; i < bytes.Length; i++) { str += string.Format("{0:X2}", bytes[i]); } } return str.ToLower(); } /// /// 从16进制转换成utf编码的字符串 /// /// 需要转码的hex /// public static byte[] UnHex(string hex) { if (hex == null) throw new ArgumentNullException("hex"); hex = hex.Replace(",", ""); hex = hex.Replace("\n", ""); hex = hex.Replace("\\", ""); hex = hex.Replace(" ", ""); if (hex.Length % 2 != 0) { hex += "20";//空格 throw new ArgumentException("hex is not a valid number!", "hex"); } // 需要将 hex 转换成 byte 数组。 byte[] bytes = new byte[hex.Length / 2]; for (int i = 0; i < bytes.Length; i++) { try { // 每两个字符是一个 byte。 bytes[i] = byte.Parse(hex.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber); } catch { // Rethrow an exception with custom message. throw new ArgumentException("hex is not a valid hex number!", "hex"); } } return bytes; } #endregion } }