饿了吗接口调试 C#版

来源:互联网 发布:三星官方网站软件下载 编辑:程序博客网 时间:2024/05/16 06:08

Program类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        private static string url_para = "/restaurant/own/";
 

        static void Main(string[] args)
        {
          
            List<model> mods = new List<model>();
            Oper op = new Oper(mods, url_para);
            Console.WriteLine(op.ElmHttpGet());
            Console.ReadKey();
        }
    }
}

Oper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
        
    class Oper
    {
        private static Dictionary<String, String> map = new Dictionary<String, String>();
        private  string sig = "";
        private  string url_para = "";
        private string url_res = "";
        private  string key = "123456789";
        private  string url = "http://v2.openapi.ele.me";
        private string timeString = "";
        private List<model> mods;

        public Oper(List<model> mods,string urlPara)
        {
            this.mods = mods;
            this.url_para = urlPara;
            map.Add("consumer_key", key);
            DateTime d1 = DateTime.Now;
            timeString = ToTimestamp(d1).ToString("0");
            map.Add("timestamp", timeString);
        }
        public double ToTimestamp(DateTime value)
        {
            TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
            return (double)span.TotalSeconds;
        }
        public string ElmHttpGet()
        {
            string con_para = "";
            foreach (model item in mods)
            {
                map.Add(item.key, item.value);
                con_para += "&" + item.key + "=" + item.value;
            }
            try
            {
                sig = ElemeHelper.genSig(url + url_para, map, "123456789/*");
                Console.WriteLine(sig);
            }
            catch (Exception e)
            {

            }
            Serlet myServlet = new Serlet();
            Console.WriteLine(sig);
            url_res = myServlet.HttpGet(url + url_para, "consumer_key=" + key + "&sig=" + sig + "&timestamp=" + timeString + con_para);
            return url_res;
        }
        public string ElmHttPost()
        {
            string con_para = "{";
            try
            {
                foreach (model item in mods)
                {
                    con_para+="'"+item.key+"':'"+item.value+"',";
                }
                con_para +="}";
            }
            catch (Exception)
            {
                
                throw;
            }
            try
            {
                sig = ElemeHelper.genSig( url+ url_para, map, "45678978979888645645647912345654");
                Console.WriteLine(sig);
            } catch (Exception e) {
                
            }
            Serlet myServlet = new Serlet();
            Console.WriteLine(sig);
            url_res = myServlet.HttpPost(url + url_para+"?consumer_key="+key+"&sig=" + sig + "&timestamp=" + timeString, con_para);
            return url_res;
        }
    }
}

ElemeHelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace ConsoleApplication1
{
    class ElemeHelper
    {
        private static String concatParams(Dictionary<String, String> params2)
        {
        Object[] key_arr = params2.Keys.ToArray();
        Array.Sort(key_arr);
        String str = "";

        int i= 0;
        foreach (Object key in key_arr) {
            string val = params2[key.ToString()];
            string keyString = Encoding.Default.GetString(Encoding.UTF8.GetBytes(key.ToString()));
            val = Encoding.Default.GetString(Encoding.UTF8.GetBytes(val));
            if (i==0)
            {
                str += key + "=" + val;
            }
            else
            {
                str += "&" + key + "=" + val;
            }
            i++;
        }

        return str;
    }

    private static  string byte2hex(byte[] b) {
        string buf = "";
        int i;

        for (int offset = 0; offset < b.Length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf+="0";
            buf+=Convert.ToString(i,16);
        }
        return buf;
    }

    public static string genSig(string pathUrl, Dictionary<String, String> params1,
                                string consumerSecret){
        string str = concatParams(params1);
        str = pathUrl + "?" + str + consumerSecret;
        Console.WriteLine("{0:d5}", str);
        return SHA1_Hash(byte2hex(Encoding.UTF8.GetBytes(str)));
    }
    //SHA1
    public static  string SHA1_Hash(string str_sha1_in)
    {
        SHA1 sha1 = new SHA1CryptoServiceProvider();
        byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str_sha1_in);
        byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
        string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
        str_sha1_out = str_sha1_out.Replace("-", "");
        return str_sha1_out.ToLower();
    }
    }
}

Serlet类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Serlet
    {
        CookieContainer cookie = new CookieContainer();
        public string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            Console.WriteLine(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        public  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;
        }
    }
}

model类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class model
    {
        public string key { get; set; }
        public string value { get; set; }
    }
}




0 0
原创粉丝点击