http://merchant.openapi.eleme.io/merchant.html#id215

 class Program
{
static void Main(string[] args)
{
EleMakSign.SignValue.Add("consumer_key", "");
//EleMakSign.SignValue.Add("consumer_secret", "87217cb263701f90316236c4df00d9352fb1da76");
EleMakSign.SignValue.Add("timestamp", "");
//EleMakSign.SignValue.Add("food_id", "20385638");
//EleMakSign.SignValue.Add("name", "肥牛套餐 + 可乐");
//EleMakSign.SignValue.Add("description", "精选五星肥牛,御用厨师:李大胖,倾情烹制");
//EleMakSign.SignValue.Add("stock", 1000);
//Root r = new Root() { key1 = "value1", key2 = "value2" }; //EleMakSign.SignValue.Add("json", JsonConvert.SerializeObject(r)); string val=EleMakSign.genMakSig("http://v2.openapi.ele.me/restaurant/62028381/", EleMakSign.SignValue, EleMeConfig.consumer_secret);
Console.Write(val); //获取订单
//
string requrl = "http://v2.openapi.ele.me/order/new/"; EleMakSign.SignValue.Clear();
string consumer_key="";
EleMakSign.SignValue.Add("consumer_key", consumer_key);
string timsp=ElemeMakSignHelper.GetTimeStamp();
EleMakSign.SignValue.Add("timestamp",timsp );
string consumer_secret="87217cb263701f90316236c4df00d9352fb1da76";
string restaurant_id="";
EleMakSign.SignValue.Add("restaurant_id", restaurant_id);
string sig=EleMakSign.genMakSig(requrl, EleMakSign.SignValue, consumer_secret); EleMakSign.SignValue.Clear();
EleMakSign.SignValue.Add("consumer_key", consumer_key);
EleMakSign.SignValue.Add("timestamp",timsp );
EleMakSign.SignValue.Add("restaurant_id", restaurant_id);
EleMakSign.SignValue.Add("sig", sig); string str = ElemeMakSignHelper.concatParams(EleMakSign.SignValue);
str = requrl + "?" + str;
string result=HttpUtilsHelper.HttpGet(str,"");
Console.Write(result);
Console.ReadKey();
//字符串a
//字符串b
//字符串c }
}
 /// <summary>
/// ele 签名
/// </summary>
public class EleMakSign
{ //对参数进行排序得到 字符串A
//将所有参数(sig除外)按照参数名的字母顺序排序,并用 & 连接:
//consumer_key=7284397484&timestamp=1374908054 public static SortedDictionary<string, object> SignValue = new SortedDictionary<string, object>(); /**
* 设置某个字段的值
* @param key 字段名
* @param value 字段值
*/
public void SetValue(string key, object value)
{
SignValue[key] = value;
} /**
* 根据字段名获取某个字段的值
* @param key 字段名
* @return key对应的字段值
*/
public object GetValue(string key)
{
object o = null;
SignValue.TryGetValue(key, out o);
return o;
} /**
* 判断某个字段是否已设置
* @param key 字段名
* @return 若字段key已被设置,则返回true,否则返回false
*/
public bool IsSet(string key)
{
object o = null;
SignValue.TryGetValue(key, out o);
if (null != o)
return true;
else
return false;
} public static string sortedToString(SortedDictionary<string, object> SignValue, string key)
{
string str = "";
foreach (var item in SignValue)
str += item + key;
return str;
} public static string genMakSig(string pathUrl, SortedDictionary<string, object> dics,
string consumerSecret)
{
string str = ElemeMakSignHelper.concatParams(dics);
str = pathUrl + "?" + str + consumerSecret;
System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
return ElemeMakSignHelper.byte2hex(sha1.ComputeHash(Encoding.UTF8.GetBytes(ElemeMakSignHelper.byte2hex(Encoding.UTF8.GetBytes(str)))));
} }

elemksign

  /// <summary>
/// 饿了么商家信息配置
/// consumer_key: 0170804777
///consumer_secret: 87217cb263701f90316236c4df00d9352fb1da76
///restaurant_id: 62028381
///restaurant_name: 饿了么开放平台测试
///餐厅下单测试地址: http://r.ele.me/openapi-test
///关于 POST / PUT 请求头中 contennt_type 的说明:
///除特别声明需要指定的请求头(如图片接口的图片内容参数 file,使用 multipart/form-data )之外,
///其他接口默认使用 application/x-www-form-urlencoded
/// </summary>
public static class EleMeConfig
{
public static string comsumer_key = "";
public static string consumer_secret = "87217cb263701f90316236c4df00d9352fb1da76";
public static int restaurant_id = ;
public static string reqBaseUrl = "http://v2.openapi.ele.me/restaurant/62028381/";
}

饿了么商家信息配置

    public static class ElemeMakSignHelper
{ public static string concatParams(SortedDictionary<string, object> dics)
{
string str = "";
foreach (var key in dics.Keys)
{
object tval = "";
if (dics.TryGetValue(key, out tval))
{
//string tkey = EleMakSign.UrlEncode(key);
//string ttval = EleMakSign.UrlEncode(tval.ToString());
string tkey = key;
string ttval = tval.ToString();
str += "&" + tkey + "=" + ttval;
}
}
return str.Substring();
} public static string byte2hex(byte[] b)
{
StringBuilder enText = new StringBuilder();
int i;
for (int offset = ; offset < b.Length; offset++)
{
i = b[offset];
if (i < )
i += ;
if (i < )
enText.Append("");
enText.Append(ElemeMakSignHelper.toHexString(i));
}
return enText.ToString();
} private static string toHexString(int i)
{
return ElemeMakSignHelper.toUnsignedString(i, );
}
private static String toUnsignedString(int i, int shift)
{
char[] buf = new char[];
int charPos = ;
int radix = << shift;
int mask = radix - ;
do
{
buf[--charPos] = digits[i & mask];
i = foo(i, shift);
} while (i != ); return new String(buf, charPos, ( - charPos));
}
public static int foo(int x, int y)
{
int mask = 0x7fffffff; //Integer.MAX_VALUE
for (int i = ; i < y; i++)
{
x >>= ;
x &= mask;
}
return x;
}
public static char[] digits = {
'' , '' , '' , '' , '' , '' ,
'' , '' , '' , '' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
}; /// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(, , , , , , );
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
}

ElemeMakSignHelper

  /// <summary>
/// http get post helper class
/// </summary>
public static class HttpUtilsHelper
{
public static string HttpGet(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8"; 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 static string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = postDataStr;
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, , payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
return strValue;
}
}

HttpUtilsHelper

完整代码在:http://download.csdn.net/detail/u010683837/9555239

饿了么 openapi demo的更多相关文章

  1. Java面试题之在多线程情况下,单例模式中懒汉和饿汉会有什么问题呢?

    懒汉模式和饿汉模式: public class Demo { //private static Single single = new Single();//饿汉模式 private static S ...

  2. OpenAPI 3.0 规范-食用指南

    概述 OpenAPI 3.0 规范由 8 个根对象组成: openapi info servers paths components security tags externalDocs OpenAP ...

  3. js引用类型数组去重-对象标记法

    前言 Js数组去重已经有很多种实现方式:包括逐个检索对比(使用Array.property.indexOf),先排序后对比,使用hash表,利用ES6中的Set()等.这些数组去重办法中速度最快的是h ...

  4. Swagger 生成 PHP API 接口文档

    Swagger 生成 PHP API 接口文档 Lumen微服务生成Swagger文档 1.概况 有同学反馈写几十个接口文档需要两天的工作量, 随着多部门之间的协作越来越频繁, 维护成本越来越高, 文 ...

  5. elementUI——主题定制

    需求: 设计三套主题色+部分图标更换: 实现方式汇总: 1.传统做法,生成多套css主题包,切换link引入路径切换href实现,参考网站:http://jui.org/: <link id=& ...

  6. 一次使用自定义 Http Header 引发的血案

    一次使用自定义 Http Header 引发的血案 HttpClient Http Header 自定义 nginx 不转发  起因 最近在整理我们产品的 OpenAPI Demo (Python.C ...

  7. 对复杂json进行处理

    {"dingtalk_corp_role_list_response":{"result":{"has_more":"false& ...

  8. graphql-binding openapi 集成demo

    类似的将openapi 转换为graphql api 的也有 https://github.com/yarax/swagger-to-graphql 基本项目 参考代码 https://github. ...

  9. Android笔记(五十三) 利用有道OPENAPI做简单的翻译demo

    先去 http://fanyi.youdao.com/openapi?path=data-mode 申请开发者key 有道api会自动将申请的单词翻译并返回为xml或者json格式,我们所需要做的就是 ...

随机推荐

  1. Redis-cluster集群【第二篇】:redis持久化

    Redis持久化原理: Redis支持两种持久化:RDB和AOF模式 一.名词解释: RDB:持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot).AOF ...

  2. 【phpcms-v9】phpcms-v9二次开发所必须知道的步骤(转载)

    一.做phpcms-v9二次开发时,我们经常需要用到如下代码,所以有必须在这里注释说明一下 defined('IN_PHPCMS') or exit('No permission resources. ...

  3. Coding上传项目步骤

    step1:在coding上面创建一个项目mybokestep2:在git 命令台中进入项目的根目录下面,使用git init创建.git文件夹和.gitigonre文件,帮组本地与远程的链接step ...

  4. linux命令--dig

    dig,和nslookup作用有些类似,都是DNS查询工具,但是却比nslookup强大 dig,其实是一个缩写,即Domain Information Groper. [我想用google-DNS来 ...

  5. golang笔记——数据类型

    25个关键字 程序声明:import, package 程序实体声明和定义:chan, const, func, interface, map, struct, type, var 程序流程控制:go ...

  6. git checkout -b 的详细讲解

    创建分支: $ git branch mybranch 切换分支: $ git checkout mybranch 创建并切换分支: $ git checkout -b mybranch 更新mast ...

  7. HDOJ 3652 B-number

    B-number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. springmvc之前后台传值

    一.向后台传值 1.项目结构 2.jar包 3.spring-config.xml <?xml version="1.0" encoding="UTF-8" ...

  9. MYSQL基础语句

    参考书籍< MySQL数据库基础与实例教程> --孔祥盛 SQL(structured query language)结构化查询语言,应用最为广泛的关系型数据库语言. MYSQL属于关系型 ...

  10. js搜索框输入提示(高效-ys8)

    <style type="text/css"> .inputbox .seleDiv { border: 1px solid #CCCCCC; display: non ...