Example #1
0
        public List <V> HttpClientDoPost <T, V>(string baseUrl, string ticket, string requestMethod, T model, out string resultMsg) //where V : new()
        {
            resultMsg = "";
            if (string.IsNullOrEmpty(baseUrl))
            {
                baseUrl = ComClass.GetWebBaseUrl();
            }
            if (string.IsNullOrEmpty(ticket))
            {
                // ticket = BasicAuthenticationAttribute.webApiAccessKey; //本网站使用,不必放在配置文件中
            }

            var handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            };

            // var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None };
            try
            {
                using (var httpclient = new HttpClient(handler))
                {
                    httpclient.BaseAddress = new Uri(baseUrl);
                    httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", ticket);
                    //   httpclient.DefaultRequestHeaders.Add("Content-Type", "application/json;charset=UTF-8");
                    Dictionary <string, object> dic = GetPropertity(model);
                    var content  = new FormUrlEncodedContent(dic.ToDictionary(k => k.Key, v => v.Value != null ? v.Value.ToString() : ""));
                    var response = httpclient.PostAsync(requestMethod, content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var responseString = response.Content.ReadAsStringAsync();

                        var resultT = GetListFromJsonString <V>(responseString.Result.ToString(), out resultMsg);
                        //  V resultT = Common.SerializerHelper.DeserializeToObject<V>(responseString);
                        return(resultT);
                    }
                    else
                    {
                        int code = (int)response.StatusCode;
                        resultMsg = "执行失败code=" + code;
                        // return default(V);
                        return(new List <V>());
                        //  return resultT response.ReasonPhrase;
                    }
                }
            }
            catch (Exception ex)
            {
                Log2Net.LogApi.WriteExceptLog(ex, "WebApiHelper.HttpClientDoPost");
                resultMsg = ex.Message;
                // return default(V);
                return(new List <V>());
            }
        }
Example #2
0
        string HttpClientDoGet <T>(string baseUrl, string ticket, string requestMethod, out string resultMsg)
        {
            resultMsg = "";

            if (string.IsNullOrEmpty(baseUrl))
            {
                baseUrl = ComClass.GetWebBaseUrl();
            }
            if (string.IsNullOrEmpty(ticket))
            {
                // ticket = BasicAuthenticationAttribute.webApiAccessKey;//本网站使用时,不必放在配置文件中
            }

            var handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            };

            try
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    client.BaseAddress = new Uri(baseUrl);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", ticket);
                    HttpResponseMessage response = client.GetAsync(requestMethod).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var resultStr = response.Content.ReadAsStringAsync();//返回的是json字符串,可反序列化得到对象
                        //  IEnumerable<T> resultModel = response.Content.ReadAsAsync<IEnumerable<T>>().Result;
                        resultMsg = "OK";
                        return(resultStr.Result);
                    }
                    else
                    {
                        int    code = (int)response.StatusCode;
                        string msg  = response.ReasonPhrase;
                        resultMsg = msg;
                        return(SerializeHelper.SerializeToString(new List <T>()));
                    }
                }
            }
            catch (Exception ex)
            {
                Log2Net.LogApi.WriteExceptLog(ex, "WebApiHelper.HttpClientDoGet");
                resultMsg = ex.Message;
                return(SerializeHelper.SerializeToString(new List <T>()));
            }
        }
Example #3
0
        public List <Log2Net.Models.SysCategory> GetUserSysCategoryAccordingRoleID(long userId = -1)
        {
            var sysCateIDs = "";

            try
            {
                var allUsersSysCategory = _cacheManager.GetCache("LgWG.LogQuery").Get("UserSysCategorys", () => GetSysCategoryForAllFromDB()) as List <UserSysCategorys>;
                sysCateIDs = allUsersSysCategory.Where(a =>
                                                       (AbpSession.UserId != null && (long)a.UserID == (long)AbpSession.UserId) ||
                                                       ((long)a.UserID == userId)

                                                       ).FirstOrDefault().SysCateIDs;
            }
            catch
            {
                sysCateIDs = (from userRole in _userRoleRepository.GetAll()
                              join role in _roleRepository.GetAll() on userRole.RoleId equals role.Id
                              where (userRole.UserId == AbpSession.UserId || userRole.UserId == userId || AbpSession.UserId == null)
                              select role.SysCateIDs).FirstOrDefault();
            }

            List <Log2Net.Models.SysCategory> sysList = new List <Log2Net.Models.SysCategory>();
            var ids = sysCateIDs.Split(',');

            if (ids.Contains(((int)(Log2Net.Models.SysCategory.ALL)).ToString()))
            {
                sysList = ComClass.GetMySysCategory();
                sysList.Remove(Log2Net.Models.SysCategory.ALL);
                return(sysList);
            }
            else
            {
                foreach (var item in ids)
                {
                    Log2Net.Models.SysCategory curEnum = (Log2Net.Models.SysCategory)Convert.ToInt32(item);
                    sysList.Add(curEnum);
                }
            }
            return(sysList);
        }