예제 #1
0
        public T Execute <T>(IRequest <T> request) where T : BaseResponse
        {
            // get service url
            var url = ConfigurationManager.AppSettings["plat_url_base"];

            if (string.IsNullOrEmpty(url))
            {
                Log.Error("configuration with name 'plat_url_base' not found");
                throw new ApplicationException("configuration with name 'plat_url_base' not found");
            }
            url = url + request.GetServiceUrl();

            // serialze request message
            var reqJson = Serialize(request);

            var reqBody = string.Format("applyCode={0}&MSG={1}", request.GetApplyCode(), HttpUtility.UrlEncode(reqJson));
            // create a guid for logging
            string logId = Guid.NewGuid().ToString("D");

            // write request message in database
            SqlLog.Write(logId, request.GetType().Name, request.GetApplyCode(), reqJson, string.Empty, ResultCode.UNKNOWN, string.Empty);

            // setting up HttpWebRequest
            var webRequest = (HttpWebRequest)WebRequest.Create(url);

            //webRequest.Proxy = new WebProxy("127.0.0.1", 8888);
            webRequest.Method      = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            string respStr = string.Empty;

            try
            {
                // post message to platform
                using (var rs = webRequest.GetRequestStream())
                {
                    var logMessage = string.Format("POST {0} to {1}\r\nApply Code:{2}\r\n{3}",
                                                   request.GetType(),
                                                   request.GetServiceUrl(),
                                                   request.GetApplyCode(),
                                                   reqJson);
                    Log.Info(logMessage);

                    var reqBytes = Encoding.Default.GetBytes(reqBody);
                    rs.Write(reqBytes, 0, reqBytes.Length);
                    rs.Close();
                }



                string resultCode    = ResultCode.UNKNOWN;
                string resultMessage = string.Empty;
                T      ret           = default(T);

                // get response
                using (var resp = (HttpWebResponse)webRequest.GetResponse())
                {
                    var respStream = resp.GetResponseStream();

                    if (respStream == null)
                    {
                        throw new WebException("GetResponseStream returned null");
                    }

                    var streamReader = new StreamReader(respStream);
                    respStr = streamReader.ReadToEnd();
                    ret     = Deserialize <T>(respStr);

                    if (ret.ResultStatus != 1)
                    {
                        resultCode    = ResultCode.PLATFORM_FAILED;
                        resultMessage = ret.ErrorMsg;
                    }
                    else
                    {
                        resultCode = ResultCode.OK;
                    }
                    SqlLog.Update(logId, respStr, resultCode, resultMessage);
                    return(ret);
                }
            }
            catch (Exception e)
            {
                SqlLog.Update(logId, respStr, ResultCode.EXCEPTION, e.Message);
                // throw;
                // return default(T);
                T ret = Activator.CreateInstance <T>();
                ret.ResultStatus = -1;
                ret.ErrorMsg     = e.Message;
                return(ret);
            }
        }