示例#1
0
        /// <summary>
        ///  打印出请求串的内容,当API中的这个函数的注释放开将会被调用。
        /// </summary>
        /// <param name="url">请求串内容</param>
        /// <param name="param">请求串的参数表单</param>
        /// <param name="method">请求的方法 get/post</param>
        static private void PrintRequest(string url, Dictionary <string, string> param, string method)
        {
            string query_string = SnsNetWork.MakeQueryString(param);

            if (method.Equals("get", StringComparison.OrdinalIgnoreCase))
            {
                url = url + "?" + query_string;
            }
            HttpContext.Current.Response.Write("<br><br>============= request info ================<br>");
            HttpContext.Current.Response.Write("method :" + method + "<br>");
            HttpContext.Current.Response.Write("url    :" + url + "<br>");
            if (method.Equals("post", StringComparison.OrdinalIgnoreCase))
            {
                HttpContext.Current.Response.Write("query_string : " + query_string + "<br>");
            }
            HttpContext.Current.Response.Write("<br>params:<br>");
            foreach (string key in param.Keys)
            {
                HttpContext.Current.Response.Write(key + " = " + param[key] + "<br>");
            }
            HttpContext.Current.Response.Write("<br><br>");
        }
示例#2
0
        public RstArray ApiMsdk(string script_name, Dictionary <string, string> param, Dictionary <string, string> qs, string method, string protocol)
        {
            RstArray result_array = new RstArray();

            string uri = SnsNetWork.MakeQueryString(qs);

            script_name = script_name + "?" + uri;

            string url = protocol + "://" + server_name + script_name;

            Dictionary <string, string> cookie = new Dictionary <string, string>();

            //通过调用以下方法,可以打印出最终发送到openapi服务器的请求参数以及url,不打印可以注释

            PrintRequest(url, param, method);

            ParamJson paramj = new ParamJson {
                appid = param["appid"], openid = param["openid"], openkey = param["openkey"], userip = param["userip"]
            };

            string param_json = JsonConvert.SerializeObject(paramj);

            // 发起请求
            result_array = SnsNetWork.MakeRequest(url, param_json, 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);
            }


            //通过调用以下方法,可以打印出调用openapi请求的返回码以及错误信息,不打印可以注释
            PrintRespond(result_array);

            return(result_array);
        }