Exemple #1
0
        /// <summary>
        /// 调用 WebAPI 服务,发送请求报文,接收响应报文后,转换成指定类型的对象
        /// </summary>
        /// <typeparam name="T">返回的对象类型</typeparam>
        /// <param name="requestObject">请求报文对象</param>
        /// <param name="result">交易结果对象</param>
        /// <returns>返回的对象</returns>
        protected T Call <T>(
            object requestObject,
            out TErrorMessage result)
        {
            string url = "";

            result = new TErrorMessage();

            switch (moduleType)
            {
            case TModuleType.Exchange:
                url =
                    string.Format(
                        "{0}{1}/{2}/{3}/{4}",
                        webAPIUrl,
                        moduleType.ToString(),
                        clientID,
                        contentType.ToString(),
                        exCode);
                break;

            default:
                string    errText = string.Format("目前不支持模块 [{0}]", moduleType.ToString());
                Exception error   = new Exception(errText);
                error.Data["ErrCode"] = 999999;
                error.Data["ErrText"] = errText;
                throw error;
            }

            switch (contentType)
            {
            case TContentType.JSON:
                break;

            default:
                string    errText = string.Format("目前不支持 [{0}] 报文格式", contentType.ToString());
                Exception error   = new Exception(errText);
                error.Data["ErrCode"] = 999999;
                error.Data["ErrText"] = errText;
                throw error;
            }

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.Method            = "POST";
            request.ContentType       = "application/stream;";
            request.KeepAlive         = false;
            request.AllowAutoRedirect = true;
            request.CookieContainer   = new CookieContainer();
            request.Timeout           = 30000; // 单位:毫秒

            try
            {
                Stream stream = request.GetRequestStream();

                #region 根据传入的指定报文格式,生成交易请求对象的相应格式的报文文本
                string content = "";
                switch (contentType)
                {
                case TContentType.JSON:
                    content = requestObject.ToJSON();
                    break;

                case TContentType.XML:
                    content = requestObject.ToXML();
                    break;
                }
                #endregion

                byte[] requestContext = Encoding.UTF8.GetBytes(content);
                stream.Write(requestContext, 0, requestContext.Length);
                stream.Flush();
                stream.Close();
                Application.DoEvents();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string          resJson  = "";
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    resJson = sr.ReadToEnd();
                }

                T rtnObject = default(T);
                switch (contentType)
                {
                case TContentType.JSON:
                    result    = JsonConvert.DeserializeObject <TErrorMessage>(resJson);
                    rtnObject = JsonConvert.DeserializeObject <T>(resJson);
                    break;
                }

                result.SourceREQContent  = content;
                result.SourceRESPContent = resJson;

                return(rtnObject);
            }
            catch (Exception error)
            {
                error.Data["ErrCode"] = 999999;
                error.Data["ErrText"] = error.Message;
                throw error;
            }
        }
Exemple #2
0
 /// <summary>
 /// 执行具体的报文发送和接收交易
 /// </summary>
 protected abstract void Communicate(out TErrorMessage errorMsg);