示例#1
0
        public static HttpResponseModel <T> SendPost <T>(this HttpClient httpClient, string url, object data)
        {
            ISerializer serializer  = Serializer();
            string      json        = serializer.Serialize(data);
            HttpContent postContent = new StringContent(json, Encoding.UTF8);

            postContent.Headers.ContentType = new MediaTypeHeaderValue("text/json");
            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, url);

            httpRequest.Content = postContent;
            HttpResponseMessage   httpResponseMessage = httpClient.SendAsync(httpRequest).Result;
            HttpResponseModel <T> httpResponse        = new HttpResponseModel <T>(httpResponseMessage.StatusCode);

            if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
            {
                var    resReadAsStringAsync = httpResponseMessage.Content.ReadAsStringAsync();
                string resJson = resReadAsStringAsync.Result;
                httpResponse.Body = resJson;
                try
                {
                    var result = serializer.Deserialize <T>(resJson);
                    httpResponse.Result = result;
                }
                catch (Exception)
                {
                }
            }
            return(httpResponse);
        }
示例#2
0
        public static HttpResponseModel <T> SendGet <T>(this HttpClient httpClient, string url)
        {
            ISerializer        serializer  = Serializer();
            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, url);

            HttpResponseMessage   httpResponseMessage = httpClient.SendAsync(httpRequest).Result;
            HttpResponseModel <T> httpResponse        = new HttpResponseModel <T>(httpResponseMessage.StatusCode);

            if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
            {
                var    resReadAsStringAsync = httpResponseMessage.Content.ReadAsStringAsync();
                string resJson = resReadAsStringAsync.Result;
                httpResponse.Body = resJson;
                try
                {
                    var result = serializer.Deserialize <T>(resJson);
                    httpResponse.Result = result;
                }
                catch (Exception)
                {
                }
            }
            return(httpResponse);
        }