Exemple #1
0
        /// <summary>
        /// 请求接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="method">请求方式</param>
        /// <param name="url">请求地址</param>
        /// <param name="data">请求数据</param>
        /// <returns></returns>
        protected MSResult <T> Request <T>(string method, MSTYPE.HOST host, string url, object data = null)
        {
            var result = _request <T>(method, host, url, data);

            if (result.IsSuccess)
            {
                return(MSResult <T> .Init(result.Data));
            }
            else
            {
                return(MSResult <T> .Init(result.Error));
            }
        }
Exemple #2
0
 public MSResult DoTrans(Action <ITransContext> act)
 {
     try
     {
         DbContext.DoWithTransaction(() =>
         {
             act(new TransContext(DbContext));
         });
         return(MSResult.Init());
     }
     catch (Exception ex)
     {
         return(MSResult.Init(ex));
     }
 }
Exemple #3
0
 public MSResult <TResult> DoTrans <TResult>(Func <ITransContext, TResult> act)
 {
     try
     {
         TResult result = default(TResult);
         result = DbContext.Session.DbContext.DoWithTransaction(() =>
         {
             return(act(new TransContext(DbContext)));
         });
         return(MSResult <TResult> .Init(result));
     }
     catch (Exception ex)
     {
         return(MSResult <TResult> .Init(ex));
     }
 }
Exemple #4
0
        /// <summary>
        /// 获取试卷下的习题列表
        /// </summary>
        /// <param name="gardenId"></param>
        /// <param name="practicePaperId"></param>
        /// <returns></returns>
        public MSResult QuestionOfPracticePaper(int gardenId, long practicePaperId, Action <List <ApiModels.QuestionPractice> > action)
        {
            var result = this.Request <List <ApiModels.QuestionPractice> >("GET", MSTYPE.HOST.LW_CLASS_INTERACTION_SERVER, "/question/practice-paper/question-of-practice-paper", new
            {
                gardenId,
                practicePaperId
            });

            if (result.flag)
            {
                action(result.data);
                return(MSResult.Init());
            }
            else
            {
                return(MSResult.Init(result.msg));
            }
        }
Exemple #5
0
        /// <summary>
        /// 获取试卷下每道题的阶段属性
        /// </summary>
        /// <param name="gardenId"></param>
        /// <param name="practicePaperId"></param>
        /// <param name="questionId"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public MSResult GetQuestionProperties(int gardenId, long practicePaperId, long questionId, Action <ApiModels.QuestionProperties> action)
        {
            var result = this.Request <ApiModels.QuestionProperties>("GET", MSTYPE.HOST.LW_CLASS_INTERACTION_SERVER, "/practice-paper-question/relation/get-question-properties", new
            {
                gardenId,
                practicePaperId,
                questionId
            });

            if (result.flag)
            {
                action(result.data);
                return(MSResult.Init());
            }
            else
            {
                return(MSResult.Init(result.msg));
            }
        }
Exemple #6
0
        /// <summary>
        /// 基础请求
        /// </summary>
        /// <param name="item"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        private MSResult _request(HttpItem item, Action <Stream> action)
        {
            var             result          = MSResult.Init(false, null);
            HttpWebResponse httpWebResponse = null;

            try
            {
                OpenRequest(item);
                httpWebResponse = (HttpWebResponse)mRequest.GetResponse();
                var code = httpWebResponse.StatusCode;
                if (code == HttpStatusCode.OK)
                {
                    result.flag = true;
                    using (var staream = httpWebResponse.GetResponseStream())
                    {
                        mCookies = mRequest.CookieContainer; //保存cookies
                        action(staream);
                    }
                }
                else
                {
                    result.msg = HttpMessage.ToMessage(code);
                }
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ConnectFailure)
                {
                    using (var staream = ex.Response.GetResponseStream())
                    {
                        using (var streamReader = new StreamReader(staream, Encoding.UTF8))
                        {
                            try
                            {
                                var json = streamReader.ReadToEnd();
                                var data = JsonHelper.ToObject <Dictionary <string, object> >(json);
                                if (data.ContainsKey("developer_message"))
                                {
                                    result.msg = (string)data["developer_message"];
                                }
                                else if (data.ContainsKey("error_description"))
                                {
                                    result.msg = (string)data["error_description"];
                                }
                                else
                                {
                                    result.msg = HttpMessage.ToMessage(ex.Status);
                                }
                            }
                            catch
                            {
                                result.msg = HttpMessage.ToMessage(ex.Status);
                            }
                        }
                    }
                }
                else
                {
                    result.msg = HttpMessage.ToMessage(ex.Status);
                }
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            finally
            {
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                    httpWebResponse = null;
                }
                Abort();
            }
            return(result);
        }