博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
百度外卖接口调试 C#版
阅读量:6220 次
发布时间:2019-06-21

本文共 3541 字,大约阅读时间需要 11 分钟。

主类

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;
        }
    }

 
 

转载地址:http://ksoja.baihongyu.com/

你可能感兴趣的文章
量子通信和大数据最有市场突破前景
查看>>
StringBuilder用法小结
查看>>
对‘初学者应该选择哪种编程语言’的回答——计算机达人成长之路(38)
查看>>
如何申请开通微信多客服功能
查看>>
Sr_C++_Engineer_(LBS_Engine@Global Map Dept.)
查看>>
非监督学习算法:异常检测
查看>>
App开发中甲乙方冲突会闹出啥后果?H5 APP 开发可以改变现状吗
查看>>
jquery的checkbox,radio,select等方法总结
查看>>
Linux coredump
查看>>
Ubuntu 10.04安装水晶(Mercury)无线网卡驱动
查看>>
Myeclipes快捷键
查看>>
我的友情链接
查看>>
ToRPC:一个双向RPC的Python实现
查看>>
netty框架的学习笔记 + 一个netty实现websocket通信案例
查看>>
我的友情链接
查看>>
nginx在reload时候报错invalid PID number
查看>>
神经网络和深度学习-第二周神经网络基础-第二节:Logistic回归
查看>>
Myeclipse代码提示及如何设置自动提示
查看>>
setTimeOut(),和setInterVal()调用函数加不加括号!!!
查看>>
c/c++中保留两位有效数字
查看>>