Ejemplo n.º 1
0
        /// <summary>
        /// POST方法(解压缩方法),json传入RequestBody
        /// </summary>
        /// <param name="url"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static HttpDataGeneric <String> HttpPostGZipByHD(String app, String url, String json)
        {
            CustomException          ei   = null;
            String                   data = HttpPostGZipWithInfo(app, url, out ei, json);
            HttpDataGeneric <String> rst  = new HttpDataGeneric <string>();

            rst.DataEntity    = data;
            rst.ExceptionInfo = ei;
            return(rst);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///  restful路径方式GET 实体数据获取(同步),同时获取状态码,按状态码识别是否成功。
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="parms">参数值数组</param>
        /// <returns></returns>
        public static HttpDataGeneric <T> GetByHD <T>(String app, String url, List <KeyValuePair <String, String> > parmList = null)
        {
            CustomException ei      = null;
            T data                  = GetWithInfo <T>(app, url, out ei, parmList);
            HttpDataGeneric <T> rst = new HttpDataGeneric <T>();

            rst.DataEntity    = data;
            rst.ExceptionInfo = ei;
            return(rst);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// POST方法,键值对传入RequestBody参数
        /// </summary>
        /// <param name="url"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static HttpDataGeneric <String> HttpPostFormUrlByHD(String app, String url, List <KeyValuePair <String, String> > list)
        {
            CustomException          ei   = null;
            String                   data = HttpPostFormUrlWithInfo(app, url, out ei, list);
            HttpDataGeneric <String> rst  = new HttpDataGeneric <string>();

            rst.DataEntity    = data;
            rst.ExceptionInfo = ei;
            return(rst);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///  形成带参数的GET Url Path地址
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="parmList">参数值列表</param>
        /// <returns></returns>
        //private static string AppendUrlWithPath(string url, params String[] parms)
        //{
        //    StringBuilder sb = new StringBuilder(url);
        //    foreach (String parm in parms)
        //    {
        //        sb.Append("/");
        //        sb.Append(parm);
        //    }

        //    return sb.ToString();
        //}
        #endregion

        #region GET 数据获取(以HttpDataGeneric<T>方式返回)

        /// <summary>
        ///  GET 字符串数据获取(同步),同时获取状态码,按状态码识别是否成功。
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="statusCode">状态码</param>
        /// <param name="parms">参数对列表</param>
        /// <returns></returns>
        public static HttpDataGeneric <String> GetStringByHD(String app, String url, List <KeyValuePair <String, String> > parmList = null)
        {
            String          result  = String.Empty;
            CustomException exInfo  = null;
            String          urlcomp = GetUrl(app, url);

            if (parmList != null && parmList.Count > 0)
            {
                urlcomp = AppendUrlWithParameter(urlcomp, parmList);
            }
            try
            {
                SetHeader();
                HttpResponseMessage response = _HttpClient.GetAsync(urlcomp).Result;
                String statusCode            = response.StatusCode.ToString();
                result = response.Content.ReadAsStringAsync().Result;
                if (response.IsSuccessStatusCode)
                {
                    GetToken(response);
                    exInfo = null;
                }
                else if (statusCode.Equals("Conflict")) //业务错误
                {
                    exInfo = JsonConvert.DeserializeObject <CustomException>(result);
                }
                else if (statusCode.Equals("PreconditionFailed")) //jwt过期
                {
                    if (DoRelogin())
                    {
                        return(GetStringByHD(app, url, parmList)); //产生新的jwt,方法重新执行
                    }
                    exInfo = new CustomException(statusCode, "at " + urlcomp, "");
                }
                else //其它错误
                {
                    exInfo = new CustomException(statusCode, "at " + urlcomp, "");
                }
            }
            catch (Exception e)
            {
                exInfo = new CustomException("服务异常", e.Message + " [" + urlcomp + "]", e.StackTrace);
                RemoveHeader();
            }
            RemoveHeader();
            HttpDataGeneric <String> data = new HttpDataGeneric <string>();

            data.DataEntity    = result;
            data.ExceptionInfo = exInfo;
            return(data);
        }