/// <summary> /// API接口下行返回 /// </summary> /// <param name="cmd">接口名称</param> /// <param name="body">接口参数</param> /// <param name="error">错误描述</param> /// <param name="source">合作方账号</param> /// <param name="secret">密钥</param> /// <param name="encrypt">加密方式</param> /// <returns></returns> public object RetApiResponse(string cmd, Dictionary <string, object> body, string error = "success", string source = "", string secret = "", string encrypt = "") { body = body ?? new Dictionary <string, object>(); if (!body.Keys.Contains("errno")) { body.Add("errno", error.ToLower() == "success" ? 0 : 1); } if (!body.Keys.Contains("error")) { body.Add("error", error); } //构建接口参数 var parmDic = new SortedDictionary <string, object>(); parmDic.Add("version", BaiduwmConsts.API_VER); parmDic.Add("timestamp", TimeUtil.SecondTicks_1970); parmDic.Add("ticket", StringUtil.GuidStr().ToUpper()); parmDic.Add("encrypt", encrypt); //合作方账号 parmDic.Add("source", source); //接口名称 parmDic.Add("cmd", string.Format("resp.{0}", cmd)); //接口参数json串 parmDic.Add("body", body.ToJson()); //构建接口签名参数并签名 var sign = SignUtil.GetBaiduSign(parmDic, secret); if (parmDic.Keys.Contains("secret")) { parmDic.Remove("secret"); } if (!parmDic.Keys.Contains("sign")) { parmDic.Add("sign", sign); } parmDic["body"] = body; return(parmDic); }
/// <summary> /// 请求API接口 /// </summary> /// <param name="source">合作方账号</param> /// <param name="secret">密钥</param> /// <param name="cmd">接口名称</param> /// <param name="body">接口参数</param> /// <param name="encrypt">加密方式</param> /// <returns></returns> public BaiduApiRetModel CallApi(string source, string secret, string cmd, Dictionary <string, object> body, string encrypt = "") { BaiduApiRetModel apiRetVal = null; body = body ?? new Dictionary <string, object>(); source = string.IsNullOrWhiteSpace(source) ? ConfigUtil.BaiduSourceId : source; secret = string.IsNullOrWhiteSpace(secret) ? ConfigUtil.BaiduSourceSecret : secret; try { //构建接口参数 var parmDic = new SortedDictionary <string, object>(); parmDic.Add("version", BaiduwmConsts.API_VER); parmDic.Add("timestamp", TimeUtil.SecondTicks_1970); parmDic.Add("ticket", StringUtil.GuidStr().ToUpper()); //加密方式,允许为空 parmDic.Add("encrypt", encrypt); parmDic.Add("secret", secret); //合作方账号 parmDic.Add("source", source); //接口名称 parmDic.Add("cmd", cmd); //接口参数json串 parmDic.Add("body", body.ToJson()); //构建接口签名参数并签名 var sign = SignUtil.GetBaiduSign(parmDic, secret); if (!parmDic.Keys.Contains("sign")) { parmDic.Add("sign", sign); } var parms = new StringBuilder(); foreach (var p in parmDic) { parms.AppendFormat("{0}={1}&", p.Key, p.Value); } var parmstr = parms.ToString().TrimEnd('&'); //执行接口请求并处理返回信息 var responseText = HttpRequestUtil.HttpPost(BaiduwmConsts.GET_BASE_API, parmstr); if (!string.IsNullOrWhiteSpace(responseText)) { var retDic = JsonUtil.ToObject <Dictionary <string, object> >(responseText); if (retDic != null && retDic["body"] != null) { apiRetVal = JsonUtil.ToObject <BaiduApiRetModel>(retDic["body"].ToString()); if (apiRetVal == null || apiRetVal.errno > 0) { LogUtil.Error(string.Format("返回错误信息:{0},请求参数:{1}", responseText, parmstr)); } else { LogUtil.Info(string.Format("调用接口:{0},成功返回信息:{1}", cmd, responseText)); } } else { LogUtil.Error(string.Format("返回错误信息:{0},请求参数:{1}", responseText, retDic["body"].ToString())); } } else { LogUtil.Error(string.Format("返回信息为空,请求参数:{0}", parmstr)); } } catch (Exception ex) { LogUtil.Error(string.Format("请求接口失败,参考信息:{0}", ex.Message)); } return(apiRetVal); }