Exemple #1
0
        /// <summary>
        /// POST方式访问WebApi
        /// </summary>
        /// <typeparam name="T_Request"></typeparam>
        /// <typeparam name="T_Result"></typeparam>
        /// <param name="address"></param>
        /// <param name="t"></param>
        /// <param name="runInBackgroud">在后台执行,除非响应事件</param>
        /// <returns></returns>
        public static RequestResult <T_Result> PostAccessApi <T_Request, T_Result>(string address, T_Request t, bool runInBackgroud = false)
        {
            string result = string.Empty;
            RequestResult <T_Result> obj     = null;
            MyHttpClienHanlder       handler = new MyHttpClienHanlder();
            HttpClient client = new HttpClient(handler);
            Uri        uri    = new Uri(BaseAddress + address);

            try
            {
                if (ApiStartRequest != null && !runInBackgroud)
                {
                    ApiStartRequest(uri, null);
                }

                HttpContent content = new StringContent(JsonConvert.SerializeObject(t));
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpResponseMessage response = client.PostAsync(uri, content).Result;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                    obj    = JsonConvert.DeserializeObject <RequestResult <T_Result> >(result);

                    if (obj.Result == ResultStatus.Success)
                    {
                    }
                    else
                    {
                        Logger.Error("保存服务器数据失败,地址=" + address, new Exception(obj.Message));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("保存服务器数据失败,地址=" + address, ex);

                throw new Exception("连接服务器失败,请检查网络。", ex);
            }
            finally
            {
                if (ApiEndRequest != null && !runInBackgroud)
                {
                    ApiEndRequest(uri, null);
                }
            }
            return(obj);
        }
Exemple #2
0
        public static RequestResult <T> GetAccessApi <T>(string baseAddress, string webAddress, string address, bool runInBackgroud = false)
        {
            BaseAddress             = baseAddress;
            WebApiVisitor.WebApiUri = webAddress;
            string             result  = string.Empty;
            RequestResult <T>  obj     = null;
            MyHttpClienHanlder handler = new MyHttpClienHanlder();
            HttpClient         client  = new HttpClient(handler);
            Uri uri = new Uri(BaseAddress + address);

            try
            {
                if (ApiStartRequest != null && !runInBackgroud)
                {
                    ApiStartRequest(uri, null);
                }

                HttpResponseMessage response = client.GetAsync(uri).Result;
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    result = response.Content.ReadAsStringAsync().Result;
                    obj    = JsonConvert.DeserializeObject <RequestResult <T> >(result);

                    if (obj.Result == ResultStatus.Success)
                    {
                    }
                    else
                    {
                        Logger.Error("获取服务器数据失败,地址=" + address + " :", new Exception(obj.Message));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("获取服务器数据失败,地址=" + address + " :", ex);
                throw;
            }
            finally
            {
                if (ApiEndRequest != null && !runInBackgroud)
                {
                    ApiEndRequest(uri, null);
                }
            }
            return(obj);
        }