zoukankan      html  css  js  c++  java
  • Http请求类

    public class HttpHelper
        {
            /// <summary>
            /// 证书校验
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="certificate"></param>
            /// <param name="chain"></param>
            /// <param name="errors"></param>
            /// <returns></returns>
            //public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            //{   
            //    // 总是接受  
            //    return true;
            //}
            /// <summary>
            /// HttpGet
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="url"></param>
            /// <returns></returns>
            public static T HttpGet<T>(string url)
            {
                Stream stream = null;
                StreamReader sr = null;
                try
                {
                    WebRequest ret = WebRequest.Create(url);
                    ret.Method = "Get";
                    ret.ContentType = "application/json;charset=UTF-8";
                    HttpWebResponse response = (HttpWebResponse)ret.GetResponse();
                    stream = ret.GetResponse().GetResponseStream();
                    sr = new StreamReader(stream, Encoding.UTF8);
                    string ApiResult = sr.ReadToEnd();
                    var t = (T)JsonConvert.DeserializeObject<T>(ApiResult);
                    return t;
                }
                catch
                {
                    throw;
                }
                finally
                {
                    stream.Close();
                    sr.Close();
                }
            }
            /// <summary>
            /// HttpPost
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="paramData"></param>
            /// <param name="postUrl"></param>
            /// <param name="Token"></param>
            /// <returns></returns>
            public static T HttpPost<T>(string paramData, string postUrl, string Token = "")
            {
                StreamReader sr = null;
                Stream stream = null;
                HttpWebResponse response = null;
                try
                {
                    byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
                                                                          //ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证
                    HttpWebRequest ret = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                    if (!string.IsNullOrEmpty(Token))
                    {
                        ret.Headers["token"] = Token;
                    }
                    ret.Method = "POST";
                    ret.ContentType = "application/json";
                    ret.ContentLength = byteArray.Length;
    
                    stream = ret.GetRequestStream();
                    stream.Write(byteArray, 0, byteArray.Length);//写入参数
                    stream.Close();
    
                    try
                    {
    
                        response = (HttpWebResponse)ret.GetResponse();
    
                    }
                    catch (WebException ex)
                    {
                        response = (HttpWebResponse)ex.Response;
    
                    }
    
                    sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    string ApiResult = sr.ReadToEnd();
    
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new WebException(ApiResult);
                    }
    
                    var t = JsonConvert.DeserializeObject<T>(ApiResult);
                    return t;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    sr.Close();
                    response.Close();
                    stream.Close();
                }
            }
    
            public static T HttpBytePost<T>(byte[] byteArray, string postUrl)
            {
                StreamReader sr = null;
                Stream stream = null;
                HttpWebResponse response = null;
                try
                {
                    //byte[] byteArray = Encoding.UTF8.GetBytes(paramData); //转化
                    HttpWebRequest ret = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                    ret.Method = "POST";
                    ret.ContentType = "application/octet-stream";
                    ret.ContentLength = byteArray.Length;
                    stream = ret.GetRequestStream();
                    stream.Write(byteArray, 0, byteArray.Length);//写入参数
                    stream.Close();
                    response = (HttpWebResponse)ret.GetResponse();
                    sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                    string ApiResult = sr.ReadToEnd();
                    var t = JsonConvert.DeserializeObject<T>(ApiResult);
                    return t;
                }
                catch (Exception ex)
                {
    
                    throw ex;
                }
                finally
                {
                    sr.Close();
                    response.Close();
                    stream.Close();
                }
            }
  • 相关阅读:
    Spring 事务管理tx,aop
    好的博客参考之Spring
    Spring 事务管理
    Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建
    ssm框架搭建
    SSH整合不错的博客
    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.my.service.ProductService] for bean with name 'productService' defi报错解决方法
    修改firefox的默认缩放比
    深入理解计算机系统笔记
    OnePlus5刷机后一直检查更新
  • 原文地址:https://www.cnblogs.com/LanHai12/p/15258065.html
Copyright © 2011-2022 走看看