Exemplo n.º 1
0
        public ApiResponse HTTPPost(string url, string method, Entity o, string sessionKey)
        {
            ActReqHandler arh    = new ActReqHandler();
            ApiResponse   apiRes = new ApiResponse();

            apiRes.SetStatusCode(arh.Status);
            //apiRes.SetResponseText(arh);

            if (arh.Status != ActReqHandler.STATUS_OK)
            {
                apiRes.SetApiSucc(false);
                apiRes.SetApiMessage(arh.Message);
                return(apiRes);
            }

            apiRes.SetApiSucc(true);
            apiRes.SetActSucc(true);
            Dictionary <string, object> dictPrms = new Dictionary <string, object>();

            if (o != null)
            {
                foreach (string key in o.Keys)
                {
                    dictPrms[key] = o.Get(key);
                }
            }

            if (String.IsNullOrEmpty(sessionKey))
            {
                sessionKey = "";
            }

            if (!string.IsNullOrEmpty(sessionKey))
            {
                dictPrms["sessionkey"] = sessionKey;
            }
            dictPrms["appId"] = WebConfig.APP_ID;

            //生成签名
            string sign = GeneratePostResponseSign(dictPrms, WebConfig.APP_Key);

            dictPrms["sign"] = sign;
            List <string> list = new List <string>();

            foreach (KeyValuePair <string, object> pair in dictPrms)
            {
                list.Add(pair.Key + "=" + WebUtil.UrlEncode(Convert.ToString(pair.Value), "utf-8"));
            }
            list.Sort();
            string str = ArrayUtil.Join(list, "&");


            //string text = HttpRequestor.Get(url + "?"+ str);
            apiRes.SetResponseText(url + "?" + str);
            return(apiRes);
        }
Exemplo n.º 2
0
        public ApiResponse Execute(ActionRequest req, string sessionKey)
        {
            string        appKey = this.AppKey;
            ActReqHandler arh    = new ActReqHandler();

            if (String.IsNullOrEmpty(appKey))
            {
                arh = new ActReqHandler();
            }
            else
            {
                arh = new ActReqHandler(appKey);
            }

            arh.AddRequest(req);

            ApiResponse apiRes = new ApiResponse();

            try
            {
                if (String.IsNullOrEmpty(sessionKey))
                {
                    arh.Execute();
                }
                else
                {
                    arh.Execute(sessionKey);
                }
            }
            catch (EndOfStreamException e) { }
            catch (WebException wex)
            {
                string code = Logger.WebError(wex, "远程调用接口异常:" + req.Action);
                apiRes.SetActSucc(false);
                apiRes.SetActMessage("发生系统异常:" + code);
                return(apiRes);
            }


            apiRes.SetStatusCode(arh.Status);
            //apiRes.SetResponseText(arh);

            if (arh.Status != ActReqHandler.STATUS_OK)
            {
                apiRes.SetApiSucc(false);
                apiRes.SetApiMessage(arh.Message);
                if (arh.Status == "SESSION_KEY_INVALID")
                {
                    apiRes.SetApiStatus(ApiResStatus.SESSION_KEY_INVALID);
                }
                else
                {
                    apiRes.SetApiStatus(ApiResStatus.ERROR);
                }
                return(apiRes);
            }

            apiRes.SetApiSucc(true);
            apiRes.SetApiStatus(ApiResStatus.OK);

            ActionResponse res = req.Response;

            if (res.StatusCode != 1)
            {
                apiRes.SetActSucc(false);
                apiRes.SetActMessage(res.Message);
                return(apiRes);
            }

            apiRes.SetActSucc(true);

            return(apiRes);
        }
Exemplo n.º 3
0
        public ApiResponse PostResponse(string url, string method, Entity o, string sessionKey)
        {
            Dictionary <string, object> dictPrms = new Dictionary <string, object>();

            if (o != null)
            {
                foreach (string key in o.Keys)
                {
                    dictPrms[key] = o.Get(key);
                }
            }

            //if (String.IsNullOrEmpty(sessionKey))
            //    sessionKey = "";

            if (sessionKey != null)
            {
                dictPrms["sessionkey"] = sessionKey;
            }
            dictPrms["app_id"] = WebConfig.APP_ID;

            //生成签名
            string sign = GeneratePostResponseSign(dictPrms, WebConfig.APP_Key);

            dictPrms["sign"] = sign;

            string        strJson = "";
            ActReqHandler arh     = new ActReqHandler();
            ApiResponse   apiRes  = new ApiResponse();

            try
            {
                strJson = this.Post(url, method, dictPrms);
            }
            catch (EndOfStreamException e) { }
            catch (WebException wex)
            {
                HttpWebResponse response     = (HttpWebResponse)wex.Response;
                Stream          stream       = response.GetResponseStream();
                string          responseText = IOUtil.StreamToString(stream);
                StringBuilder   sbError      = new StringBuilder();
                sbError.Append("远程调用接口异常:" + method);
                sbError.AppendLine(responseText);
                string code = Logger.Error(sbError.ToString());

                apiRes.SetActSucc(false);
                apiRes.SetActMessage("发生系统异常:" + code);
                return(apiRes);
            }
            catch (Exception ex)
            {
                apiRes.SetActSucc(false);
                apiRes.SetActMessage(ex.Message);
                return(apiRes);
            }
            apiRes.SetStatusCode(arh.Status);
            //apiRes.SetResponseText(arh);

            if (arh.Status != ActReqHandler.STATUS_OK)
            {
                apiRes.SetApiSucc(false);
                apiRes.SetApiMessage(arh.Message);
                return(apiRes);
            }

            apiRes.SetApiSucc(true);
            apiRes.SetActSucc(true);
            JavaScriptSerializer jss = new JavaScriptSerializer();

            try
            {
                //将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
                Dictionary <string, object> dict = jss.Deserialize <Dictionary <string, object> >(strJson);
                int    intResult = 0;
                object objResult;
                if (dict.TryGetValue("result", out objResult))
                {
                    intResult = Convert.ToInt32(objResult);
                }
                else
                {
                    intResult = 1;
                }
                if (intResult <= 0)
                {
                    if (dict.TryGetValue("code", out objResult).Equals("10003"))
                    {
                        apiRes.SetActSucc(false);
                        apiRes.SetActMessage(dict["msg"].ToString());
                        return(apiRes);
                    }
                    else
                    {
                        //apiRes.SetApiSucc(false);
                        apiRes.SetApiMessage(dict["msg"].ToString());
                        //return apiRes;
                    }
                }
                apiRes.SetResponseText(strJson);
            }
            catch (Exception ex)
            {
                apiRes.SetApiSucc(false);
                apiRes.SetApiMessage(ex.Message);
                return(apiRes);
            }
            return(apiRes);
        }
Exemplo n.º 4
0
        public ApiResponse PostResponse(string url, string method, Entity o, string sessionKey)
        {
            Dictionary<string, object> dictPrms = new Dictionary<string, object>();
            if (o != null)
            {
                foreach (string key in o.Keys)
                    dictPrms[key] = o.Get(key);
            }

            //if (String.IsNullOrEmpty(sessionKey))
            //    sessionKey = "";

            if(sessionKey != null)
                dictPrms["sessionkey"] = sessionKey;
            dictPrms["app_id"] = WebConfig.APP_ID;

            //生成签名
            string sign = GeneratePostResponseSign(dictPrms, WebConfig.APP_Key);
            dictPrms["sign"] = sign;

            string strJson = "";
            ActReqHandler arh = new ActReqHandler();
            ApiResponse apiRes = new ApiResponse();
            try
            {
                strJson = this.Post(url, method, dictPrms);
            }
            catch (EndOfStreamException e) { }
            catch (WebException wex)
            {
                HttpWebResponse response = (HttpWebResponse)wex.Response;
                Stream stream = response.GetResponseStream();
                string responseText = IOUtil.StreamToString(stream);
                StringBuilder sbError = new StringBuilder();
                sbError.Append("远程调用接口异常:" + method);
                sbError.AppendLine(responseText);
                string code = Logger.Error(sbError.ToString());

                apiRes.SetActSucc(false);
                apiRes.SetActMessage("发生系统异常:" + code);
                return apiRes;
            }
            catch (Exception ex)
            {
                apiRes.SetActSucc(false);
                apiRes.SetActMessage(ex.Message);
                return apiRes;
            }
            apiRes.SetStatusCode(arh.Status);
            //apiRes.SetResponseText(arh);

            if (arh.Status != ActReqHandler.STATUS_OK)
            {
                apiRes.SetApiSucc(false);
                apiRes.SetApiMessage(arh.Message);
                return apiRes;
            }

            apiRes.SetApiSucc(true);
            apiRes.SetActSucc(true);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                //将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
                Dictionary<string, object> dict = jss.Deserialize<Dictionary<string, object>>(strJson);
                int intResult = 0;
                object objResult;
                if (dict.TryGetValue("result", out objResult))
                {
                    intResult = Convert.ToInt32(objResult);
                }
                else
                {
                    intResult = 1;
                }
                if (intResult <= 0)
                {
                    if (dict.TryGetValue("code", out objResult).Equals("10003"))
                    {
                        apiRes.SetActSucc(false);
                        apiRes.SetActMessage(dict["msg"].ToString());
                        return apiRes;
                    }
                    else
                    {
                        //apiRes.SetApiSucc(false);
                        apiRes.SetApiMessage(dict["msg"].ToString());
                        //return apiRes;
                    }
                }
                apiRes.SetResponseText(strJson);
            }
            catch (Exception ex)
            {
                apiRes.SetApiSucc(false);
                apiRes.SetApiMessage(ex.Message);
                return apiRes;
            }
            return apiRes;
        }
Exemplo n.º 5
0
        public ApiResponse HTTPPost(string url, string method, Entity o, string sessionKey)
        {
            ActReqHandler arh = new ActReqHandler();
            ApiResponse apiRes = new ApiResponse();
            apiRes.SetStatusCode(arh.Status);
            //apiRes.SetResponseText(arh);

            if (arh.Status != ActReqHandler.STATUS_OK)
            {
                apiRes.SetApiSucc(false);
                apiRes.SetApiMessage(arh.Message);
                return apiRes;
            }

            apiRes.SetApiSucc(true);
            apiRes.SetActSucc(true);
            Dictionary<string, object> dictPrms = new Dictionary<string, object>();
            if (o != null)
            {
                foreach (string key in o.Keys)
                    dictPrms[key] = o.Get(key);
            }

            if (String.IsNullOrEmpty(sessionKey))
                sessionKey = "";

            if (!string.IsNullOrEmpty(sessionKey)) { dictPrms["sessionkey"] = sessionKey; }
            dictPrms["appId"] = WebConfig.APP_ID;

            //生成签名
            string sign = GeneratePostResponseSign(dictPrms, WebConfig.APP_Key);
            dictPrms["sign"] = sign;
            List<string> list = new List<string>();
            foreach (KeyValuePair<string, object> pair in dictPrms)
                list.Add(pair.Key + "=" + WebUtil.UrlEncode(Convert.ToString(pair.Value), "utf-8"));
            list.Sort();
            string str = ArrayUtil.Join(list, "&");

            //string text = HttpRequestor.Get(url + "?"+ str);
            apiRes.SetResponseText(url + "?" + str);
            return apiRes;
        }
Exemplo n.º 6
0
        public ApiResponse Execute(ActionRequest req, string sessionKey)
        {
            string appKey = this.AppKey;
            ActReqHandler arh = new ActReqHandler();
            if (String.IsNullOrEmpty(appKey))
                arh = new ActReqHandler();
            else
                arh = new ActReqHandler(appKey);

            arh.AddRequest(req);

            ApiResponse apiRes = new ApiResponse();

            try
            {
                if (String.IsNullOrEmpty(sessionKey))
                    arh.Execute();
                else
                    arh.Execute(sessionKey);
            }
            catch (EndOfStreamException e) { }
            catch (WebException wex)
            {
                string code = Logger.WebError(wex, "远程调用接口异常:" + req.Action);
                apiRes.SetActSucc(false);
                apiRes.SetActMessage("发生系统异常:" + code);
                return apiRes;
            }

            apiRes.SetStatusCode(arh.Status);
            //apiRes.SetResponseText(arh);

            if (arh.Status != ActReqHandler.STATUS_OK)
            {
                apiRes.SetApiSucc(false);
                apiRes.SetApiMessage(arh.Message);
                if (arh.Status == "SESSION_KEY_INVALID")
                    apiRes.SetApiStatus(ApiResStatus.SESSION_KEY_INVALID);
                else
                    apiRes.SetApiStatus(ApiResStatus.ERROR);
                return apiRes;
            }

            apiRes.SetApiSucc(true);
            apiRes.SetApiStatus(ApiResStatus.OK);

            ActionResponse res = req.Response;
            if (res.StatusCode != 1)
            {
                apiRes.SetActSucc(false);
                apiRes.SetActMessage(res.Message);
                return apiRes;
            }

            apiRes.SetActSucc(true);

            return apiRes;
        }