using ExtensionMethods; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; using SAL_OUTSTOCK.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Security.Cryptography; using System.Text; namespace UnitTestProject1 { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { string appKey = "437268ee6e1a44b6ba135eea1614f94d"; string secret = "72CD0295-2312-4B08-9A4C-2A338A0EE9BF"; // 业务参数 Dictionary jsonMap = new Dictionary(); jsonMap.Add("declaCode", "T00001"); jsonMap.Add("entCusCode", "4404660006"); jsonMap.Add("entCreditCode", "91440404MABRGQJR1H"); jsonMap.Add("entName", "珠海昶展物流有限公司"); jsonMap.Add("declaEntCusCode", "4404660006"); jsonMap.Add("declaEntCreditCode", "91440404MABRGQJR1H"); jsonMap.Add("declaEntName", "珠海昶展物流有限公司"); jsonMap.Add("customsCode", "5781"); jsonMap.Add("bookNum", "T5781W000004"); jsonMap.Add("compileDate", "1970-01-01 08:00:00"); jsonMap.Add("inputMan", "Test01"); jsonMap.Add("declaType", "00"); jsonMap.Add("inputDate", "1970-01-01 08:00:00"); jsonMap.Add("declaDate", "1970-01-01 08:00:00"); var auditList = new List>(); Dictionary data = new Dictionary(); data.Add("itemNo", "1"); data.Add("deliveryOrderNo", "2"); data.Add("productCd", "100242"); data.Add("productDesc", "喇叭"); data.Add("specificationsModels", " "); data.Add("deliveryCnt", "80.0000000000"); data.Add("calcUnit", "Pcs"); data.Add("hscode", "8"); data.Add("inventory", "9"); data.Add("inventoryOrderNo", "10"); Dictionary data1 = new Dictionary(); data1.Add("itemNo", "2"); data1.Add("deliveryOrderNo", "2"); data1.Add("productCd", "100243"); data1.Add("productDesc", "听筒"); data1.Add("specificationsModels", " "); data1.Add("deliveryCnt", "80.0000000000"); data1.Add("calcUnit", "Pcs"); data1.Add("hscode", "8"); data1.Add("inventory", "9"); data1.Add("inventoryOrderNo", "10"); auditList.Add(data); jsonMap.Add("itemList", auditList); string json = JsonConvert.SerializeObject(jsonMap); json = WebUnit.UrlEncode(json); // 系统参数 Dictionary param = new Dictionary(); param.Add("name", "supvWarehouse.save"); param.Add("app_key", appKey); param.Add("data", json); param.Add("timestamp", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); param.Add("version", ""); string sign = BuildSign(param, secret); param.Add("sign", sign); var ttt = @"{""entName"":""珠海昶展物流有限公司"",""entCusCode"":""4404660006"",""bookNum"":""T5781W000004"",""declaEntCreditCode"":""91440404MABRGQJR1H"",""deliveryType"":""QTCK"",""declaEntCusCode"":""4404660006"",""inputDate"":""2018-02-02 14:03:07"",""deliveryTm"":""2018-02-09 00:00:00"",""declaType"":""00"",""declaDate"":""2023-12-20 09:38:55"",""declaCode"":""QTCK000003"",""itemList"":[{""productDesc"":""喇叭"",""inventoryOrderNo"":""10"",""deliveryOrderNo"":""2"",""specificationsModels"":"" "",""hscode"":""8"",""deliveryCnt"":""80.0000000000"",""itemNo"":""1"",""inventory"":""9"",""productCd"":""100242"",""calcUnit"":""Pcs""},{""productDesc"":""听筒"",""inventoryOrderNo"":""10"",""deliveryOrderNo"":""2"",""specificationsModels"":"" "",""hscode"":""8"",""deliveryCnt"":""80.0000000000"",""itemNo"":""2"",""inventory"":""9"",""productCd"":""100243"",""calcUnit"":""Pcs""}],""entCreditCode"":""91440404MABRGQJR1H"",""declaEntName"":""珠海昶展物流有限公司"",""inputMan"":""demo"",""customsCode"":""5781""}"; //var json = "{\"entName\":\"珠海昶展物流有限公司\",\"entCusCode\":\"4404660006\",\"bookNum\":\"T5781W000004\",\"declaEntCreditCode\":\"91440404MABRGQJR1H\",\"declaEntCusCode\":\"4404660006\",\"entCreditCode\":\"91440404MABRGQJR1H\",\"declaEntName\":\"珠海昶展物流有限公司\",\"inputMan\":\"demo\",\"customsCode\":\"5781\",\"inputDate\":\"2018-02-02 14:03:07\",\"declaType\":\"00\",\"deliveryTm\":\"2018-02-09 00:00:00\",\"declaDate\":\"2023-12-20 10:55:59\",\"deliveryType\":\"QTCK\",\"declaCode\":\"QTCK000003\",\"item\":[{\"itemNo\":\"1\",\"deliveryOrderNo\":\"2\",\"productCd\":\"100242\",\"productDesc\":\"喇叭\",\"specificationsModels\":\" \",\"deliveryCnt\":\"80.0000000000\",\"calcUnit\":\"Pcs\",\"hscode\":\"8\",\"inventory\":\"9\",\"inventoryOrderNo\":\"10\"},{\"itemNo\":\"2\",\"deliveryOrderNo\":\"2\",\"productCd\":\"100243\",\"productDesc\":\"听筒\",\"specificationsModels\":\" \",\"deliveryCnt\":\"80.0000000000\",\"calcUnit\":\"Pcs\",\"hscode\":\"8\",\"inventory\":\"9\",\"inventoryOrderNo\":\"10\"}]}"; var ttt2 = JsonConvert.SerializeObject(param); HttpWebHelper.DoPost("http://119.146.223.48:39082/api/", ttt2); var a = 0.00M; var a2 = 0L; var f = (a == a2); return; } public static string BuildSign(Dictionary paramsMap, string secret) { //var keySet = paramsMap.Keys; List paramNames = paramsMap.Keys.ToList(); paramNames.Sort(); StringBuilder paramNameValue = new StringBuilder(); foreach (var paramName in paramNames) { paramNameValue.Append(paramName).Append(paramsMap[paramName]); } string source = secret + paramNameValue.ToString() + secret; return ToMD5(source); } public static string ToMD5(string message) { try { // 1 创建一个提供信息摘要算法的对象,初始化为md5算法对象 MD5 md5 = new MD5CryptoServiceProvider(); // 2 将消息变成byte数组 byte[] input = Encoding.UTF8.GetBytes(message); // 3 计算后获得字节数组,这就是那128位了 byte[] buff = md5.ComputeHash(input); // 4 把数组每一字节(一个字节占八位)换成16进制连成md5字符串 return Byte2hex(buff); } catch (Exception e) { throw e; } } private static string Byte2hex(byte[] bytes) { //创建一个StringBuilder对象来存储最终的结果 StringBuilder sign = new StringBuilder(); //使用for循环遍历字节数组中的每个字节 for (int i = 0; i < bytes.Length; i++) { // 将字节强制转换为整数,并对其进行位运算,即保留后八位的值,并转换为十六进制形式的字符串 string hex = bytes[i].ToString("x2"); // 如果转换后的字符串长度为1,即只有1位,则在前面添加一个0,以保证每个字节都是两位十六进制数 if (hex.Length == 1) { sign.Append("0"); } // 将转换后的字符串追加到StringBuilder对象中,并转换为大写形式 sign.Append(hex.ToUpper()); } return sign.ToString(); } } }