On a project to use signing algorithm, the interface is written like this:
5.1.1 Input parameters Method = openapi.stockout.create timestamp = , 2015 - 04 - 26 is 00 : 00 : 07 the format = XML app_key = testerp_appkey V = 2.0 sign_method = MD5 the customerId = Test 5.1.2 . Alphabetical ascending app_key = testerp_appkey the customerId = cust- Stub- code the format = XML Method = openapi.stockout.create sign_method = MD5 timestamp = , 2015 - 04 - 26 is 00 : 00 : 07 V = 2.0 5.1.3 connection string. connection parameter names and parameter values, and inclusive plus Secret, assumed here Secret = Test, as follows: testapp_keytesterp_appkeycustomerIdstub - cust- codeformatxmlmethod openapi.stockout.createsign_methodmd5timestamp2015 - 04 - 26 is 00 : 00 .: 07v2 0 body Test wherein: body with body data request instead 5.1.4 generate the signature Sign. 32 uppercase MD5 value ->D06D88CB34B2EC0E5C9BAB396C9542B6 5.1.5 . Assembling the URL request to convert all parameter values UTF -8 encoding , then assembled, and the ENCODING URL as follows: HTTP: // sandbox.vwms.cn/open/service?method= opeanapi.stockout.create & timestamp = 2015 - 04 - 26 % 2000 : 00 : 07 & format = xml & app_key = testerp_appkey & v = 2.0 & Sign = D06D88CB34B2EC0E5C9B AB396C9542B6 & sign_method = MD5 & customerId Stub-cust-code =
Practice is this:
//timestamp string timestamp = DateTime.Now.ToString(); JObject o = new JObject( new JProperty("app_key", _Setting.Value.app_key.ToString()), new JProperty("customerId", _Setting.Value.customerId.ToString()), new JProperty("format", "xml"), new JProperty("method", " Openapi.singleitem.synchronize " ), new new JProperty ( " sign_method " , " MD5 " ), new new JProperty ( " timestamp " , timestamp), new new JProperty ( " V " , " 2.0 " ) ); // put above sort String S = JsonConvert.SerializeObject (O); the Dictionary <string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(s); string Sort_Ret = CommonData.GetSignature(values, ""); //拼接URL链接 string secret = _Setting.Value.secret; string Sign = CommonData.StrToMD5(secret + Sort_Ret + in0 + secret); string url = "" + _Setting.Value.UrlApi.ToString() + "?" + "method=" + "openapi.singleitem.synchronize" + "×tamp=" + timestamp + "" + "&format=xml" + "&app_key=" + _Setting.Value.app_key.ToString() + "" + "&v=2.0" + "&sign=" + Sign + "" + "&sign_method=md5" + "&customerId="_Setting.Value.customerId.ToString + () + "" ; // send Http request HttpClient Client = httpClientFactory.CreateClient (); HttpContent Content = new new StringContent object, (IN0, Encoding.UTF8 , "file application / JSON"); // The ownership of utf-8 encoded var RET = the await client.PostAsync (URL, Content); var RR = the await ret.Content.ReadAsStringAsync ();
The method involved, is a sort of a Sign value is obtained
public static String GetSignature (the IDictionary < String , String > Parameters, String Secret) { // first parameter lexicographical sort ascending its parameter name the IDictionary < String , String > = sortedParams new new the SortedDictionary < String , String > (Parameters) ; the IEnumerator <KeyValuePair < String , String >> Iterator = sortedParams.GetEnumerator (); // traversing the dictionary sorted, all the parameters as "keyvaluekeyvalue" formats are spliced together StringBuilder basestring =new StringBuilder(); while (iterator.MoveNext()) { string key = iterator.Current.Key; string value = iterator.Current.Value.ToString(); if (!string.IsNullOrEmpty(key) && value != null) { basestring.Append(key).Append(value); } } // 使用MD5对待签名串求签 //string upperCaseStr = StrToMD5(basestring.ToString()); //string result = StrToMD5(upperCaseStr + secret); return basestring.ToString(); } public static string StrToMD5(string str) { byte[] data = Encoding.UTF8.GetBytes(str); MD5 md5 = new MD5CryptoServiceProvider(); byte[] OutBytes = md5.ComputeHash(data); string OutString = ""; for (int i = 0; i < OutBytes.Length; i++) { OutString += OutBytes[i].ToString("x2"); } return OutString.ToUpper(); } public static DateTime GetLocaltime(string StandardTime) { var ChTimeZone = TimeZoneInfo.FindSystemTimeZoneById(StandardTime); DateTime ChTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, ChTimeZone); return ChTime; }