Esempio n. 1
0
        /// <summary>
        /// Post请求返回实体
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求数据</param>
        /// <returns>实体</returns>
        public static T PostResponse <T>(string url, string postData)
        {
            HttpClient httpClient = new HttpClient(new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            });
            HttpResponseMessage response = null;

            try
            {
                //using (HttpClient httpClient = new HttpClient())
                //{
                httpClient.MaxResponseContentBufferSize = 256000;
                httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
                httpClient.CancelPendingRequests();
                httpClient.DefaultRequestHeaders.Clear();
                HttpContent httpContent = new StringContent(postData);

                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                T result = default(T);
                Task <HttpResponseMessage> taskResponse = httpClient.PostAsync(url, httpContent);
                taskResponse.Wait();
                response = taskResponse.Result;
                //using (HttpResponseMessage response = taskResponse.Result)
                //{
                //HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                if (response.IsSuccessStatusCode)
                {
                    //response.Content.ReadAsAsync<IEnumerable<ProductInfo>>().Result
                    //Task<string> t = response.Content.ReadAsStringAsync();
                    //t.Wait();
                    //string s = t.Result;
                    Task <System.IO.Stream> taskStream = response.Content.ReadAsStreamAsync();
                    taskStream.Wait();
                    System.IO.Stream       dataStream = taskStream.Result;
                    System.IO.StreamReader reader     = new System.IO.StreamReader(dataStream);
                    string s = reader.ReadToEnd();
                    result = JsonConvertTool.DeserializeObject <T>(s);
                }
                //}
                return(result);
                //}
            }
            catch
            {
                return(default(T));
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Post请求返回字符
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="postData">请求数据</param>
        /// <returns>字符</returns>
        public static string PostResponse(string url, object obj)
        {
            HttpClient httpClient = new HttpClient(new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            });
            HttpResponseMessage response = null;

            try
            {
                //using (HttpClient httpClient = new HttpClient())
                //{
                httpClient.MaxResponseContentBufferSize = 256000;
                httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
                httpClient.CancelPendingRequests();
                httpClient.DefaultRequestHeaders.Clear();
                string      postData    = JsonConvertTool.SerializeObject(obj);
                HttpContent httpContent = new StringContent(postData);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                Task <HttpResponseMessage> taskResponse = httpClient.PostAsync(url, httpContent);
                taskResponse.Wait();
                response = taskResponse.Result;
                //using (HttpResponseMessage response = taskResponse.Result)
                //{
                //HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                if (response.IsSuccessStatusCode)
                {
                    //Task<string> taskStr = response.Content.ReadAsStringAsync();
                    //taskStr.Wait();
                    //string result = taskStr.Result;

                    Task <System.IO.Stream> taskStream = response.Content.ReadAsStreamAsync();
                    taskStream.Wait();
                    System.IO.Stream       dataStream = taskStream.Result;
                    System.IO.StreamReader reader     = new System.IO.StreamReader(dataStream);
                    string result = reader.ReadToEnd();
                    return(result);
                }
                else
                {
                    LogHelper.WriteInfo(typeof(HttpClientHelper), "结果(Http):" + response.StatusCode + "内容:" + JsonConvertTool.SerializeObject(obj));
                }
                //}
                return(null);
                //}
            }
            catch (Exception exception)
            {
                LogHelper.Error("结果:异常 内容:" + JsonConvertTool.SerializeObject(obj) + "ex:" + exception.Message, exception);
                return(null);
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 根据Url地址Get请求返回实体
        ///
        /// 2015年11月12日14:50:02
        /// </summary>
        /// <param name="url">请求的地址</param>
        /// <returns>实体</returns>
        public static T GetResponse <T>(string url)
        {
            HttpClient httpClient = new HttpClient(new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip
            });
            HttpResponseMessage response = null;

            try
            {
                //using (HttpClient httpClient = new HttpClient())
                //{
                //httpClient.MaxResponseContentBufferSize = 256000;
                httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
                httpClient.CancelPendingRequests();
                httpClient.DefaultRequestHeaders.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                //HttpResponseMessage response = httpClient.GetAsync(url).Result;
                Task <HttpResponseMessage> taskResponse = httpClient.GetAsync(url);
                taskResponse.Wait();
                T result = default(T);
                response = taskResponse.Result;
                //using (HttpResponseMessage response = taskResponse.Result)
                //{
                if (response.IsSuccessStatusCode)
                {
                    //Task<string> t = response.Content.ReadAsStringAsync();
                    //t.Wait();
                    //string s = t.Result;


                    Task <System.IO.Stream> taskStream = response.Content.ReadAsStreamAsync();
                    taskStream.Wait();
                    System.IO.Stream       dataStream = taskStream.Result;
                    System.IO.StreamReader reader     = new System.IO.StreamReader(dataStream);
                    string s = reader.ReadToEnd();

                    result = JsonConvertTool.DeserializeObject <T>(s);
                }
                //}
                return(result);
                //}
            }
            catch (Exception e)
            {
                LogHelper.WriteLog(typeof(HttpClientHelper), "GetResponse", "", null, e);
                var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = new StringContent(e.ToString()),
                    ReasonPhrase = "error"
                };
                //throw new HttpResponseException(resp);
                return(default(T));
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
            }
        }