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();
                }
            }
  • 相关阅读:
    覆盖一个DIV,让radio、checkbox和select只读
    DNN 配置 数据库篇
    Javascript实用语句收录
    绑定Hashtable到DataList
    WebDeploymentSetup 合并程序集时出错(ILMerge.Merge: ERROR)
    只能向页面中添加 ScriptManager 的一个实例
    让DNN添加的别名起作用
    360极速模式(Chrome内核)下由ashx输出的JavaScript代码不起作用
    C#中常用到的时间函数(天数差、星期几等)
    C#实用语句
  • 原文地址:https://www.cnblogs.com/LanHai12/p/15258065.html
Copyright © 2011-2022 走看看