static public string MakeQueryString(Dictionary <string, string> param) { if (param.Count == 0) { return(""); } string query_string = ""; foreach (string key in param.Keys) { query_string = query_string + SnsSigCheck.UrlEncode(key, Encoding.UTF8) + "=" + SnsSigCheck.UrlEncode(param[key], Encoding.UTF8) + "&"; } query_string = query_string.Substring(0, query_string.Length - 1); return(query_string); }
/// <summary> /// 执行API调用,返回结果数组 /// </summary> /// <param name="script_name">调用的API方法,比如/v3/user/get_info, 参考 http://wiki.open.qq.com/wiki/API_V3.0%E6%96%87%E6%A1%A3 </param> /// <param name="param">调用API时带的参数</param> /// <param name="method">请求方法 post / get</param> /// <param name="protocol">协议类型 http / https </param> /// <returns>返回结果数组</returns> public RstArray Api(string script_name, Dictionary <string, string> param, string method, string protocol) { RstArray result_array = new RstArray(); // 检查 openid 是否为空 if (string.IsNullOrEmpty(param["openid"])) { result_array.Msg = "openid is empty"; result_array.Ret = OPENAPI_ERROR_REQUIRED_PARAMETER_EMPTY; return(result_array); } // 检查 openid 是否合法 if (!IsOpenId(param["openid"])) { result_array.Msg = "openid is invalid"; result_array.Ret = OPENAPI_ERROR_REQUIRED_PARAMETER_INVALID; return(result_array); } // 无需传sig, 会自动生成 if (param.ContainsKey("sig")) { param.Remove("sig"); } // 添加一些参数 param["appid"] = appid.ToString(); param["format"] = format; // 生成签名 string secret = appkey + "&"; string sig = SnsSigCheck.MakeSig(method, script_name, param, secret); param.Add("sig", sig); string url = protocol + "://" + server_name + script_name; // 增加cookie Dictionary <string, string> cookie = new Dictionary <string, string>(); //记录接口调用开始时间 long start_time = SnsStat.GetTime(); //通过调用以下方法,可以打印出最终发送到openapi服务器的请求参数以及url,不打印可以注释 PrintRequest(url, param, method); // 发起请求 result_array = SnsNetWork.MakeRequest(url, param, cookie, method, protocol); if (result_array.Ret != 0) { result_array.Ret += OPENAPI_ERROR_HTPP; return(result_array); } //解析返回结果的返回码 string stat_ret = ""; try { if (this.format == "xml") { XmlDocument xml = new XmlDocument(); xml.LoadXml(result_array.Msg); stat_ret = xml.LastChild["ret"].InnerText.ToString(); } else { JavaScriptSerializer serializer = new JavaScriptSerializer(); RstArray json_obj = new RstArray(); json_obj = serializer.Deserialize <RstArray>(result_array.Msg); stat_ret = json_obj.Ret.ToString(); } } catch (Exception e) { result_array.Msg = e.Message; // 远程返回的不是 json或者xml 格式, 说明返回包有问题 result_array.Ret += OPENAPI_ERROR_RESPONSE_DATA_INVALID; return(result_array); } // 统计上报 if (is_stat) { Dictionary <string, string> stat_params = new Dictionary <string, string>(); stat_params["appid"] = appid.ToString(); stat_params["pf"] = param["pf"]; stat_params["rc"] = stat_ret; stat_params["svr_name"] = server_name; stat_params["interface"] = script_name; stat_params["protocol"] = protocol; stat_params["method"] = method; SnsStat.StatReport(stat_url, start_time, stat_params); } //通过调用以下方法,可以打印出调用openapi请求的返回码以及错误信息,不打印可以注释 ///PrintRespond(result_array); return(result_array); }