zoukankan      html  css  js  c++  java
  • 百度外卖接口调试 C#版

    主类

    class Program
        {
            static void Main(string[] args)
            {
                string cmdStr = "order.list";
                int soureStr = 36524;//此处填入账户
                string secertStr = "123131243245454534";//此处填入密钥
                DateTime d1 = DateTime.Now;
                
                string timeString = ToTimestamp(d1).ToString("0");
                string ticket = System.Guid.NewGuid().ToString().ToUpper();
                Sing sin = new Sing();
                sin.cmd = cmdStr;
                sin.timestamp = timeString;
                sin.version = 2;
                sin.ticket = ticket;
                sin.source = soureStr;
                sin.secret = secertStr;
                Shop shop = new Shop();
                shop.end_time = 1442559208;
                shop.page = 1;
                shop.start_time = 1442558000;
                shop.status = "1,10";
                sin.body = shop;
                string js = JsonConvert.SerializeObject(sin);
                //对所有/进行转义
                js = js.Replace("/", "\/");
                //中文字符转为unicode
                js = chinaToUnicode(js);
                Console.WriteLine(js);
                string content = CmdSerializer.ToMD5(js);
                Console.WriteLine(content);
                Cmd cmd = new Cmd();
                cmd.body = shop;
                cmd.cmd = cmdStr;
                cmd.timestamp = timeString;
                cmd.version = 2;
                cmd.ticket = ticket;
                cmd.source = soureStr;
                cmd.sign = content;

                string data = JsonConvert.SerializeObject(cmd);
                data = data.Replace("/", "\/");
                //中文字符转为unicode
                data = chinaToUnicode(data);
                Console.WriteLine(data);

                byte[] utf8 = Encoding.UTF8.GetBytes(data);
                data = Encoding.UTF8.GetString(utf8);
                string ret = Ser.HttpPost("http://api.waimai.baidu.com", data);
                Console.WriteLine(ret);
                Console.ReadKey();
            }
            static double ToTimestamp(DateTime value)
            {
                TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
                return (double)span.TotalSeconds;
            }
            public static String chinaToUnicode(String str)
            {
                String result = "";
                for (int i = 0; i < str.Length; i++)
                {
                    int chr1 = (char)str[i];
                    if (chr1 >= 19968 && chr1 <= 171941)
                    {//汉字范围 u4e00-u9fa5 (中文)
                        result += "\u" + Convert.ToString(int.Parse(chr1.ToString()), 16); ;
                    }
                    else
                    {
                        result += str[i];
                    }
                }
                return result;
            }
        }

    shop类

    class Shop
        {
            public int end_time { get; set; }
            public int page { get; set; }
            public int start_time { get; set; }
            public string status { get; set; }
            //public string order_id{ get; set; }
            //public string name { get; set; }
        }

    Cmd类

    class Cmd
        {
            public string cmd { get; set; }
            public string timestamp { get; set; }
            public int version { get; set; }
            public string ticket { get; set; }
            public int source { get; set; }
            public string sign { get; set; }
            public object body { get; set; }
        }

    CmdSerializer类

    class CmdSerializer
        {
            
            public static string ToMD5(string value)
            {
                if (value == null || value == "")
                {
                    return "";
                }
                byte[] data = System.Text.Encoding.Default.GetBytes(value);
                var md = new MD5CryptoServiceProvider();
                var data2 = md.ComputeHash(data);
                var v = BitConverter.ToString(data2);
                v = v.Replace("-", "");
                v = v.ToUpper();
                return v;
            }
        }

    Sing类

    class Sing
        {
            public object body { get; set; }
            public string cmd { get; set; }
            public string secret { get; set; }
            public int source { get; set; }
            public string ticket { get; set; }
            public string timestamp { get; set; }
            public int version { get; set; }
        }

    Ser类

    class Ser
        {
            public static string HttpPost(string url, string context)
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Timeout = 60 * 1000;
                req.ReadWriteTimeout = 60 * 1000;
                //
                byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(context);
                req.Method = "POST";
                Stream requestStream = req.GetRequestStream();
                requestStream.Write(requestBytes, 0, requestBytes.Length);
                requestStream.Close();
                //
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                Stream stream = res.GetResponseStream();
                StreamReader reader = new StreamReader(stream);
                string str = reader.ReadToEnd();
                stream.Close();
                reader.Close();
                return str;
            }
        }

     
     
  • 相关阅读:
    Backbone源码解析(六):观察者模式应用
    NodeJs 开发微信公众号(五)真实环境部署
    NodeJs 开发微信公众号(四)微信网页授权
    NodeJs 开发微信公众号(三)微信事件交互
    NodeJs 开发微信公众号(二)测试环境部署
    NodeJs 开发微信公众号(一)准备工作
    Css 动画的回调
    GIT常用命令笔记
    论如何在手机端web前端实现自定义原生控件的样式
    Box-sizing:小身材,大拳头!
  • 原文地址:https://www.cnblogs.com/soundcode/p/7521155.html
Copyright © 2011-2022 走看看