百度外卖接口调试 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;
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咖飞_coffee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值