Пример #1
0
        /// <summary>
        /// 发出 Put 请求
        /// </summary>
        /// <typeparam name="T">响应报文反序列类型</typeparam>
        /// <param name="url">请求路径</param>
        /// <param name="body">请求数据,将会被 json 序列化后放到请求报文体中
        /// <param name="requestHeaders">请求额外的报文头信息</param>
        /// <returns></returns>
        public async Task <RestResponseWithBody <T> > PutForEntityAsync <T>(String url, object
                                                                            body = null, HttpRequestHeaders requestHeaders = null)
        {
            using (HttpRequestMessage requestMsg = new HttpRequestMessage())
            {
                if (requestHeaders != null)
                {
                    foreach (var header in requestHeaders)
                    {
                        requestMsg.Headers.Add(header.Key, header.Value);
                    }
                }
                requestMsg.Method = System.Net.Http.HttpMethod.Put;
                //http://apiservice1/api/values 转换为 http://192.168.1.1:5000/api/values
                requestMsg.RequestUri = new Uri(await ResolveUrlAsync(url));
                requestMsg.Content    = new
                                        StringContent(JsonConvert.SerializeObject(body));
                requestMsg.Content.Headers.ContentType = new
                                                         MediaTypeHeaderValue("application/json");
                RestResponseWithBody <T> respEntity = await
                                                      SendForEntityAsync <T>(requestMsg);

                return(respEntity);
            }
        }
Пример #2
0
        /// <summary>
        /// 发出 Http 请求
        /// </summary>
        /// <typeparam name="T">响应报文反序列类型</typeparam>
        /// <param name="requestMsg">请求数据</param>
        /// <returns></returns>
        public async Task <RestResponseWithBody <T> > SendForEntityAsync <T>(HttpRequestMessage requestMsg)
        {
            var result = await httpClient.SendAsync(requestMsg);

            RestResponseWithBody <T> respEntity = new RestResponseWithBody <T>();

            respEntity.StatusCode = result.StatusCode;
            respEntity.Headers    = respEntity.Headers;
            String bodyStr = await result.Content.ReadAsStringAsync();

            if (!string.IsNullOrWhiteSpace(bodyStr))
            {
                respEntity.Body = JsonConvert.DeserializeObject <T>(bodyStr);
            }
            return(respEntity);
        }
Пример #3
0
        public async Task <RestResponseWithBody <T> > GetForEntityAsync <T>(string url, HttpRequestHeaders requestHeaders = null)
        {
            using (HttpRequestMessage requestMsg = new HttpRequestMessage())
            {
                if (requestHeaders != null)
                {
                    foreach (var item in requestHeaders)
                    {
                        requestMsg.Headers.Add(item.Key, item.Value);
                    }
                }
                requestMsg.Method     = System.Net.Http.HttpMethod.Get;
                requestMsg.RequestUri = new Uri(await ResolveUrlAsync(url));
                RestResponseWithBody <T> respEntity = await SendForEntityAsync <T>(requestMsg);

                return(respEntity);
            }
        }
Пример #4
0
        /// <summary>
        /// 发出Get请求
        /// </summary>
        /// <typeparam name="T">响应报文反序列类型</typeparam>
        /// <param name="url">请求路径</param>
        /// <param name="requestHeaders">请求额外的报文头信息</param>
        /// <returns></returns>
        public async Task <RestResponseWithBody <T> > GetForEntityAsync <T>(String url, HttpRequestHeaders requestHeaders = null)
        {
            using (HttpRequestMessage requestMsg = new HttpRequestMessage())
            {
                if (requestHeaders != null)
                {
                    foreach (var header in requestHeaders)
                    {
                        requestMsg.Headers.Add(header.Key, header.Value);
                    }
                }
                requestMsg.Method = System.Net.Http.HttpMethod.Get;
                //http://apiservice1/api/values转换为http://192.168.1.1:5000/api/values
                requestMsg.RequestUri = new Uri(await ResolveUrlAsync(url));
                RestResponseWithBody <T> respEntity = await SendForEntityAsync <T>(requestMsg);

                return(respEntity);
            }
        }