zoukankan      html  css  js  c++  java
  • C# 调用LAKALA接口获取静态二维码数据

    本实例是通过WinForm程序(C#开发)调用的拉卡拉静态二维码接口,获取静态二维码信息,具体调用过程如下:

    • 定义基本对象类
     public class orderExtInfoData
        {
        }
     public class reqDataData
        {
            public string termId { get; set; }
            public string shopNo { get; set; }
        }
     public class termExtInfoData
        {
        }
      public class PostLakalaData
        {
            public string ver { get; set; }
            public string appid { get; set; }
            public string sign { get; set; }
            public string channel { get; set; }
            public string rnd { get; set; }
            public string cmd { get; set; }
            public string productType { get; set; }
            public string token { get; set; }
            public string timestamp { get; set; }
            public string reqId { get; set; }
            public reqDataData reqData = new reqDataData();
            public orderExtInfoData orderExtInfo = new orderExtInfoData();
            public termExtInfoData termExtInfo = new termExtInfoData();
        }
    • 整理提交接口数据
         /// <summary>
            /// 获取PostData数据
            /// </summary>
            /// <returns></returns>
            public string  GetPostLakalaData()
            {
                string secretKey = "f6cc7030b8c0ba07a6da488362f2748e";
                PostLakalaData postData = new PostLakalaData();
                postData.ver = "1.0.0";
                postData.appid = "qm1624980136"; //拉卡拉授权提供
                postData.productType = "LKL_APP_QR";//拉卡拉授权提供
                postData.token = "43e99b4a38a748d3932fca9382404b41";//拉卡拉授权提供
                postData.cmd = "QR_ORDER_BIND_CODE_CREATE";//指令
                postData.channel = "LAKALA";//渠道
              
                postData.rnd = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 32);//随机数
                postData.timestamp = DateTimeUtil.Timestamp(DateTime.Now);//时间戳
                postData.reqId = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 32);//请求序列(具体传值待确认)
                reqDataData reqDataDataref = new reqDataData();
                reqDataDataref.termId = txttermId.Text;// 拉卡拉授权提供;
                reqDataDataref.shopNo = txtshopNo.Text;//拉卡拉授权提供;
                postData.reqData= reqDataDataref;
                postData.sign = GetSign(reqDataDataref, secretKey);//对reqData,进行签名,注意不能有空格
                orderExtInfoData oeinfodata = new orderExtInfoData();
                postData.orderExtInfo=oeinfodata;
                termExtInfoData termExtInfodata = new termExtInfoData();
                postData.termExtInfo=termExtInfodata;
                return  JsonConvert.SerializeObject(postData); 
            }
       /// <summary>
            /// 转换Sign数据
            /// </summary>
            /// <param name="reqDataDataref"></param>
            /// <param name="secretKey"></param>
            /// <returns></returns>
            private string GetSign(reqDataData reqDataDataref,string secretKey)
            {
                if (reqDataDataref == null)
                    return "";
                string sign = "shopNo="+ reqDataDataref.shopNo+ "" +  "|termId=" + reqDataDataref.termId+ "|"+ secretKey;
                return GetMD5(sign).ToUpper();
            }
       /// <summary>
            /// Md5加密
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static string GetMD5(string s)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] t = md5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(s));
                StringBuilder sb = new StringBuilder(32);
                for (int i = 0; i < t.Length; i++)
                {
                    sb.Append(t[i].ToString("x").PadLeft(2, '0'));
                }
                return sb.ToString();
            }
    • 提交调用数据
          private void button1_Click(object sender, EventArgs e)
            {
                string statuscode = "";
                string reslutdata = HttpClientHelper.PostResponse(txtUrl.Text,txtPostData.Text,out statuscode); 
                txtResponseData.Text = reslutdata;
                JObject obj = (JObject)JsonConvert.DeserializeObject(reslutdata);
                ResponseLakalaData rk = new ResponseLakalaData();
                rk.retCode = obj["retCode"].ToString();
                rk.retMsg = obj["retMsg"].ToString();
                string redatasss = obj["respData"].ToString();
                JObject objreq = (JObject)JsonConvert.DeserializeObject(redatasss);
                rk.reqsData = new respLakalaData();
                rk.reqsData.code = objreq["code"].ToString();
                rk.reqsData.codeImage = objreq["codeImage"].ToString();
                txtStatusCode.Text = statuscode;
                MessageBox.Show(rk.reqsData.code);
            }
     public class HttpClientHelper
        {
            public static string PostResponse(string url, string postData, out string statusCode)
            {
                string result = string.Empty;
                //设置Http的正文
                HttpContent httpContent = new StringContent(postData);
                //设置Http的内容标头
                httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                //设置Http的内容标头的字符
                httpContent.Headers.ContentType.CharSet = "utf-8";
                using (HttpClient httpClient = new HttpClient())
                {
                    //异步Post
                    HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                    //输出Http响应状态码
                    statusCode = response.StatusCode.ToString();
                    //确保Http响应成功
                    if (response.IsSuccessStatusCode)
                    {
                        //异步读取json
                        result = response.Content.ReadAsStringAsync().Result;
                    }
                }
                return result;
            }
           
        }

     

       

        第一次做拉卡拉接口调用的功能,调用的接口功能虽然简单,但是网上并没有合适的资料供参考(拉卡拉也不给提供一个),故自行摸索逐一实现,发表出来是希望对做Winform开发的朋友提供一个参考,避免一些不必要的坑,比如对SIGN的加密过程、PostResponse的调用实现等。

  • 相关阅读:
    scipy.spatial.distance.cdist
    关于hstack和Svstack
    numpy.hstack(tup)
    numpy.random.uniform(记住文档网址)
    Python集合(set)类型的操作
    python+Eclipse+pydev环境搭建
    python数据挖掘领域工具包
    LVS 命令使用
    CMD mysql 备份脚本
    Windos Server Tomcat 双开配置
  • 原文地址:https://www.cnblogs.com/sbjl/p/11326160.html
Copyright © 2011-2022 走看看