예제 #1
0
        /// <summary>
        /// 向服务器发送get请求  返回服务器回复数据(string)
        /// </summary>
        /// <param name="uri">API地址</param>
        /// <returns></returns>
        public static async Task <string> GetAsync(string uri)
        {
            try
            {
                HttpResponseMessage response = await httpClient.GetAsync(new Uri(uri));

                if (response.StatusCode == Windows.Web.Http.HttpStatusCode.Unauthorized)
                {
                    OnUnAuthorized?.Invoke();
                }

                response.EnsureSuccessStatusCode();
                return(await response.Content.ReadAsStringAsync());
            }
            catch (Exception e)
            {
                LogHelper.WriteLine(e);
                return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// 向服务器发送Post请求,参数放在参数字典中
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static async Task <string> PostDicAsync(Dictionary <string, string> dic, string uri)
        {
            try
            {
                HttpFormUrlEncodedContent content  = new HttpFormUrlEncodedContent(dic);
                HttpResponseMessage       response = await httpClient.PostAsync(new Uri(uri), content);

                if (response.StatusCode == Windows.Web.Http.HttpStatusCode.Unauthorized)
                {
                    OnUnAuthorized?.Invoke();
                }

                response.EnsureSuccessStatusCode();
                return(await response.Content.ReadAsStringAsync());
            }
            catch (Exception e)
            {
                LogHelper.WriteLine(e);
                return(null);
            }
        }
예제 #3
0
        /// <summary>
        /// 向服务器发送post请求 返回服务器回复数据(string)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static async Task <string> PostAsync(string uri, string body)
        {
            try
            {
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(uri));
                request.Content = new HttpStringContent(body, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json; charset=utf-8");
                HttpResponseMessage response = await httpClient.SendRequestAsync(request);

                if (response.StatusCode == Windows.Web.Http.HttpStatusCode.Unauthorized)
                {
                    OnUnAuthorized?.Invoke();
                }

                response.EnsureSuccessStatusCode();
                return(await response.Content.ReadAsStringAsync());
            }
            catch (Exception e)
            {
                LogHelper.WriteLine(e);
                return(null);
            }
        }