查询类demo(C#)

#### 测试环境系统参数及接口调用地址:

sid         = 'apidev2';
appkey      = 'xxx2test';
appsecret   = '12345';

service_url = 'http://121.41.177.115/openapi2/trade_query.php';
/*trade_query.php此为接口名示例,根据实际对接的接口进行变更*/

正式环境系统参数及接口调用地址:

sid         = '卖家帐号';
appkey      = '卖家账号-ot 或 卖家账号-gw';
appsecret   = 'xxxxxx';
/*appsecret销售向公司申请后提供给商家*/

service_url = 'http://api.wangdian.cn/openapi2/trade_query.php';
/*trade_query.php此为接口名示例,根据实际对接的接口进行变更*/

查询类demo(C#)

     Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("sid", TransConstConfig.ErpAppId);
            dic.Add("appkey", TransConstConfig.ErpAppKey);
            dic.Add("timestamp", TimeConverter.ToUtp(DateTime.Now).ToString());

            dic.Add("limit", limit.ToString());
            return CreateParam(dic, true);

        public string CreateParam(Dictionary<string, string> dicReq,bool isLower=false)
        {
            //排序
            dicReq = dicReq.OrderBy(r => r.Key).ToDictionary(r => r.Key, r => r.Value);

            StringBuilder sb = new StringBuilder();
            int i = 0;
            foreach (var item in dicReq)
            {
                if (item.Key == "sign")
                    continue;
                if (i > 0)
                {
                    sb.Append(";");
                }
                i++;
                sb.Append(item.Key.Length.ToString("00"));
                sb.Append("-");
                sb.Append(item.Key);
                sb.Append(":");

                sb.Append(item.Value.Length.ToString("0000"));
                sb.Append("-");
                sb.Append(item.Value);
            }
            if (isLower)
                dicReq.Add("sign", MD5Encrypt(sb + TransConstConfig.ErpAppsecret).ToLower());
            else
                dicReq.Add("sign", MD5Encrypt(sb + TransConstConfig.ErpAppsecret));
            sb = new StringBuilder();
            i = 0;
            foreach (var item in dicReq)
            {
                if (i == 0)
                    sb.Append(string.Format("{0}={1}", item.Key, HttpUtility.UrlEncode(item.Value, Encoding.UTF8)));
                else
                    sb.Append(string.Format("&{0}={1}", item.Key, HttpUtility.UrlEncode(item.Value, Encoding.UTF8)));
                i++;
            }

            return sb.ToString();
        }

        public static string MD5Encrypt(string strText)
        {
            MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strText));
            string strMd5 = BitConverter.ToString(result);
            strMd5 = strMd5.Replace("-", "");
            return strMd5;// System.Text.Encoding.Default.GetString(result);
        }

        public string Post(string url,string postData)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream serviceRequestBodyStream = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Credentials = CredentialCache.DefaultCredentials;
                request.KeepAlive = false;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";

                UTF8Encoding encoding = new UTF8Encoding();
                byte[] bodyBytes = encoding.GetBytes(postData);
                request.ContentLength = bodyBytes.Length;
                using (serviceRequestBodyStream = request.GetRequestStream())
                {
                    serviceRequestBodyStream.Write(bodyBytes, 0, bodyBytes.Length);
                    serviceRequestBodyStream.Close();
                    using (response = (HttpWebResponse)request.GetResponse())
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string result = reader.ReadToEnd();
                            reader.Close();
                            return result;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteError(ex, "Post");
                throw;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }

        }
Print Friendly, PDF & Email