首页 > 编程语言 >C#实现HTTP访问类HttpHelper

C#实现HTTP访问类HttpHelper

时间:2022-09-05 16:34:52浏览次数:61  
标签:HTTP string C# HttpHelper Add webRe public resultLog 请求

在项目开发过程中,我们经常会访问第三方接口,如我们需要接入的第三方接口是Web API,这时候我们就需要使用HttpHelper调用远程接口了。示例中的HttpHelper类使用Log4Net记录了每次调用的请求内容和响应内容的日志,并且每条日志都带上了链路ID和标识,这样方便我们在排查问题时能快速的找到当时的请求和响应内容,进而定位分析问题。大家在使用的时候如不需要记录日志,删除掉即可。

HttpHelper类代码如下:

    public class HttpHelper : IDisposable
    {
        private bool _disposable = false;
        /// <summary>
        /// 请求编码格式默认utf-8;
        /// </summary>
        public Encoding HtmlEncoding = Encoding.UTF8;
        /// <summary>
        /// 请求时间
        /// </summary>
        public int Timeout = 5000;

        public CookieContainer Cookies = null;
        /// <summary>
        /// 是否记录Cookies
        /// </summary>
        public bool IsRecordCookie = false;

        public string ContentType = "application/x-www-form-urlencoded";

        public string AcceptLanguage = "en-US, en; q=0.8, zh-Hans-CN; q=0.5, zh-Hans; q=0.3";

        public string KeepAlive = "Keep-Alive";

        public string Accept = "*/*";

        private const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240";

        private static ILogger Logger = Log4NetLoggerFactory.Instance.Create("remote.info");

        public HttpHelper()
        {
            //允许最大连接数,突破Http协议的并发连接数限制
            ServicePointManager.DefaultConnectionLimit = 512;
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="url"></param>
        /// <param name="bArr"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public HttpRequestEntity RequestFile(string url, byte[] bArr, string fileName = "")
        {
            var result = new HttpRequestEntity { IsSuccess = 0 };
            //后续需要再放开,启用时需增加日志收集
            //if (string.IsNullOrEmpty(url))
            //    throw new ArgumentNullException("请求Url不能为空值");

            //if (bArr == null || bArr.Length <= 0)
            //    throw new AccessViolationException("缺少输入数据");

            //Stream requestStream = null;
            //StreamReader streamReader = null;
            //HttpWebResponse response = null;
            //HttpWebRequest request = null;
            //try
            //{
            //    request = WebRequest.Create(url) as HttpWebRequest;
            //    request.AllowAutoRedirect = true;
            //    request.Method = "POST";
            //    string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
            //    request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
            //    byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
            //    byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");

            //    if (string.IsNullOrEmpty(fileName))
            //        fileName = DateTime.Now.ToString("yyyyMMddHHmmss");

            //    //请求头部信息 
            //    StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
            //    byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
            //    request.Headers.Add("auth", fileName);
            //    Stream postStream = request.GetRequestStream();
            //    postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
            //    postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
            //    postStream.Write(bArr, 0, bArr.Length);
            //    postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
            //    postStream.Close();
            //    response = request.GetResponse() as HttpWebResponse;
            //    requestStream = response.GetResponseStream();
            //    if (response.StatusCode == HttpStatusCode.OK)
            //    {
            //        result.IsSuccess = 0;
            //        if (requestStream != null)
            //        {
            //            streamReader = new StreamReader(requestStream, HtmlEncoding);
            //            result.ResponseContent = streamReader.ReadToEnd();
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    result.IsSuccess = 1;
            //    result.ResponseContent = ex.Message;
            //}
            //finally
            //{
            //    if (requestStream != null)
            //    {
            //        requestStream.Close();
            //        requestStream.Dispose();
            //    }

            //    if (streamReader != null)
            //    {
            //        streamReader.Close();
            //        streamReader.Dispose();
            //    }

            //    request.Abort();
            //    if (response != null)
            //        response.Close();

            //}

            return result;
        }

        /// <summary>
        /// 基本请求方法
        /// </summary>
        /// <param name="requestType">HTTP请求类型</param>
        /// <param name="url">请求的URL</param>
        /// <param name="requestData">请求参数</param>
		/// <param name="traceID">链路ID,方便查询日志</param>
		/// <param name="markType">请求标识,方便查询日志</param>
        /// <returns></returns>
        private HttpRequestEntity BaseRequest(RequestType requestType, string url, string requestData, string traceID,string markType)
        {
            var result = new HttpRequestEntity { IsSuccess = 0 };

            if (string.IsNullOrEmpty(url))
                throw new ArgumentNullException("请求Url不能为空值");

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Dictionary<string, object> resultLog = new Dictionary<string, object>();//log对象
            resultLog.Add("logType", "remote");
            resultLog.Add("traceID", traceID);
            resultLog.Add("localIp", IpHelper.LocalIp);
            resultLog.Add("markType", markType);
            resultLog.Add("url", url);            
            resultLog.Add("requestContent", HttpUtility.UrlDecode(requestData, Encoding.UTF8));
            resultLog.Add("createTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            StackTrace ss = new StackTrace(true);
            System.Reflection.MethodBase mb = ss.GetFrame(2).GetMethod();//0表示当前栈空间,1表示上一级的栈空间,依次类推
            resultLog.Add("className", mb.DeclaringType.FullName);
            resultLog.Add("methodName", mb.Name);
            HttpStatusCode statusCode = HttpStatusCode.OK;

            if (IsRecordCookie)
                Cookies = new CookieContainer();
            Stream requestStream = null;
            StreamReader streamReader = null;

            HttpWebRequest webRe = null;
            HttpWebResponse webPos = null;
            try
            {
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    webRe = WebRequest.Create(url) as HttpWebRequest;
                    webRe.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    webRe = (HttpWebRequest)WebRequest.Create(url);
                }

                webRe.Headers.Add("Accept-Language", AcceptLanguage);
                webRe.Headers.Add("Keep-Alive", KeepAlive);
                webRe.UserAgent = UserAgent;
                webRe.Accept = Accept;
                webRe.Timeout = Timeout;
                webRe.ReadWriteTimeout = Timeout;
                webRe.CookieContainer = Cookies;

                if (requestType == RequestType.Post)
                {
                    webRe.ContentType = string.Format("{0}; {1}", ContentType, HtmlEncoding.BodyName);
                    byte[] datas = HtmlEncoding.GetBytes(requestData);
                    webRe.Method = "POST";
                    webRe.ContentLength = datas.Length;
                    webRe.MaximumResponseHeadersLength = -1;
                    requestStream = webRe.GetRequestStream();
                    requestStream.Write(datas, 0, datas.Length);
                    requestStream.Flush();
                    requestStream.Close();
                }
                else
                    webRe.Method = "GET";

                webPos = (HttpWebResponse)webRe.GetResponse();
                resultLog.Add("requestType", webRe.Method);
                statusCode = webPos.StatusCode;
                result.ResponseLength = webPos.ContentLength;
                result.ResponseEncodingName = webPos.ContentEncoding;

                requestStream = webPos.GetResponseStream();
                if (webPos.StatusCode == HttpStatusCode.OK)
                {
                    result.IsSuccess = 0;

                    if (requestStream != null)
                    {
                        streamReader = new StreamReader(requestStream, HtmlEncoding);
                        result.ResponseContent = streamReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                result.IsSuccess = 1;
                result.ResponseContent = ex.Message;
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                    requestStream.Dispose();
                }

                if (streamReader != null)
                {
                    streamReader.Close();
                    streamReader.Dispose();
                }

                webRe.Abort();
                if (webPos != null)
                    webPos.Close();

            }
            if (result.IsSuccess == 1)
            {
                resultLog.Add("status", HttpStatusCode.InternalServerError);
                resultLog.Add("success", false);
                resultLog.Add("responseContent", result.ResponseContent);
                stopwatch.Stop();
                resultLog.Add("elapseTime", stopwatch.Elapsed.TotalMilliseconds);
                string log = JsonConvert.SerializeObject(resultLog);
                Logger.Info(log);
                Logger.Error(log);
            }
            else
            {
                resultLog.Add("status", statusCode);
                resultLog.Add("success", true);
                resultLog.Add("responseContent", result.ResponseContent);
                stopwatch.Stop();
                resultLog.Add("elapseTime", stopwatch.Elapsed.TotalMilliseconds);
                string log = JsonConvert.SerializeObject(resultLog);
                Logger.Info(log);
            }
            return result;
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受  
        }

        /// <summary>
        /// Get请求
        /// </summary>
        /// <param name="url">请求地址</param>
		/// <param name="traceID">链路ID,方便查询日志</param>
		/// <param name="markType">请求标识,方便查询日志</param>
        /// <returns></returns>
        public HttpRequestEntity Request(string url, string traceID, string markType)
        {
            return BaseRequest(RequestType.Get, url, string.Empty, traceID, markType);
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url">请求地址Url</param>
        /// <param name="requestData">请求内容参数</param>
		/// <param name="traceID">链路ID,方便查询日志</param>
		/// <param name="markType">请求标识,方便查询日志</param>
        /// <returns></returns>
        public HttpRequestEntity Request(string url, string requestData, string traceID, string markType)
        {
            return BaseRequest(RequestType.Post, url, requestData, traceID, markType);
        }

        ~HttpHelper()
        {
            Dispose(false);
        }

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (this._disposable)
                return;

            if (disposing)
            {

            }

            _disposable = true;
        }

        #endregion
    }

    /// <summary>
    /// HttpHelper请求方式
    /// </summary>
    public enum RequestType
    {
        /// <summary>
        /// Get请求
        /// </summary>
        Get,
        /// <summary>
        /// Post请求
        /// </summary>
        Post
    }

    /// <summary>
    /// HttpHelper请求时返回实体
    /// </summary>
    public class HttpRequestEntity
    {
        /// <summary>
        /// 请求是否成功 0-成功(返回Http状态码200) 1-失败(出现异常)
        /// </summary>
        public int IsSuccess { get; set; }
        /// <summary>
        /// 请求返回内容
        /// </summary>
        public string ResponseContent { get; set; }
        /// <summary>
        /// 请求返回内容长度
        /// </summary>
        public long ResponseLength { get; set; }
        /// <summary>
        /// 请求返回编码类型
        /// </summary>
        public string ResponseEncodingName { get; set; }
    }

调用示例如下:

HttpHelper helper = new HttpHelper();
HttpRequestEntity response = helper.Request("需要访问的URL", "请求需要的参数", "访问链路ID", "访问标识");
if (response.IsSuccess != 0)
{
	//程序处理异常,请重试!
}
else
{
	//请求响应成功	
}

 


如对您有帮助劳烦帮忙点个赞,收藏关注一下,相互学习,共同进步。

标签:HTTP,string,C#,HttpHelper,Add,webRe,public,resultLog,请求
From: https://www.cnblogs.com/huaxiaorui/p/16658624.html

相关文章

  • 使用RestHighLevelClient的3种分页实现
    目录from+size分页1.1from+size的命令行实现1.2from+size的RestHighLevelClient实现scroll分页2.1scroll分页的命令行实现2.2scroll的RestHighLevelClient实......
  • 如何用AscendCL的接口开发网络模型推理场景下应用?
    摘要:本期我们来深入讲解网络模型推理场景下,具体怎么做。本文分享自华为云社区《【CANN文档速递09期】应用开发之推理场景》,作者:昇腾CANN。我们知道,使用AscendCL接口开......
  • 题解【CF1316E Team Building】
    题目传送门状压DP入门题。设\(f_{i,S}\)表示考虑了前\(i\)个人,队伍放置情况为\(S\)时(0表示放置了队员,1表示没有放置)的最大贡献。然后分讨一下\(i\)是去当队......
  • react向路由组件传递参数数据的3种方式
    1、params传递参数步骤:(1)路由链接(携带参数)<Linkto={`/home/message/detail/${ele.id}/${ele.title}`}>{ele.title}</Link>(2)注册路由(声明接收):<Routepath='/home......
  • dct
    https://blog.csdn.net/cv_rsrc/article/details/124697564 https://blog.csdn.net/weixin_48946798/article/details/125451230......
  • JavaScript 特殊数字值 NaN
    NaN是唯一一个不和自身不相等的值,Array》prototype.indexOf使用了严格相等,因此不能通过该方法在数组中查找NaN:NaN===NaN//false[NaN].indexof(NaN);//false如......
  • RPC是什么,看完你就知道了
    RPC概述RPC是什么RPC(RemoteProcedureCall)远程过程调用协议,一种通过网络从远程计算机上请求服务,而不需要了解底层网络技术的协议。RPC它假定某些协议的存在,例如TPC/UDP......
  • OSI七层模型与TCP/IP协议
    一、分层的优点二、七层模型三、TCP/IP参考模型四、理想的网络设计五、数据的封装过程1、为什么分层层次划分的必要性:很多不同的厂家生产各种型号的计算机,它们运行......
  • 八、Spring Boot 中自定义 SpringMVC 配置
    转发:https://www.javaboy.org/2019/0816/spring-boot-springmvc.html先说结论,使用Java8的,自定义配置使用实现WebMvcConfigurer接口,Java8之前使用WebMvcConfigurerAdapte......
  • C语言:随机访问fseek()和ftell()
    随机访问fseek()和ftell()有了fseek()函数,便可把文件看作是数组,在fopen()打开的文件中直接移动到任意字节处。下面代码演示了fseek()和ftell()的用法:#include<stdio.h>......