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();
                }
            }
  • 相关阅读:
    SQL SERVER 2012修改数据库名称(包括 db.mdf 名称的修改)
    vmware三种网络模式
    指针
    linux 中 开放端口,以及防火墙的相关命令
    数据库备份的脚本,记录下,还需优化下
    遍历 目录的几种有效办法
    转。git 乌龟的使用安装
    centos 时区正确,时间不对
    locate
    从 零开始 无差错 装好nginx+PHP
  • 原文地址:https://www.cnblogs.com/LanHai12/p/15258065.html
Copyright © 2011-2022 走看看