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