tnblog
首页
登录

封装调用api接口的通用方法(抱含存调用接口日志)

145人阅读 2019/8/27 13:25 评论:0 手机 收藏 关注
分类: webAPI
      protected static readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// get请求
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="uri"></param>
        /// <param name="content_type"></param>
        /// <returns></returns>
        public static T Get<T>(string uri, string content_type = "application/json; charset=utf-8")
        {
            WebClient wc = new WebClient();
            //没有链接凭证
            wc.Credentials = CredentialCache.DefaultCredentials;
            //有连接凭证(没有连接凭证的可以不加)
            wc.Credentials = new NetworkCredential("用户名", "密码", "域");
            wc.Headers.Add(HttpRequestHeader.ContentType, content_type);
 
            try
            {
                byte[] text = wc.DownloadData(uri);
                string str = System.Text.Encoding.UTF8.GetString(text);
                _log.Info(" visit url:" + uri);
                return JsonConvert.DeserializeObject<T>(str);
            }
            catch (Exception ex)
            {
                _log.Error(string.Format(" uri:{0}访问异常  ", uri), ex);
                throw ex;
            }
        }
 
        /// <summary>
        /// post请求
        /// </summary>
        /// <typeparam name="T">返回的对象</typeparam>
        /// <param name="model">请求的对象</param>
        /// <param name="uri">接口地址</param>
        /// <param name="token">token没有可以去掉</param>
        /// <param name="content_type"></param>
        /// <returns></returns>
        public static T Post<T>(object model, string uri, string token = "", string content_type = "application/json; charset=utf-8")
        {
            WebClient wc = new WebClient();  
            wc.Headers.Add(HttpRequestHeader.ContentType, content_type);
            //没有链接凭证
            wc.Credentials = CredentialCache.DefaultCredentials;
            //有连接凭证(没有连接凭证的可以不加)
            wc.Credentials = new NetworkCredential("用户名", "密码", "域");
            wc.Headers.Add("token", token);
            byte[] postData = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
            //var content=new FormUrlEncodedContent() .netframwork 4.5
            try
            {
                byte[] text = wc.UploadData(uri, "post", postData);
                string str = System.Text.Encoding.UTF8.GetString(text);
                _log.Info("request:" + JsonConvert.SerializeObject(model) + " visit url:" + uri + " token:" + token);
                return JsonConvert.DeserializeObject<T>(str);
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("request:{1} token:{2} uri:{0}访问异常  ", uri, JsonConvert.SerializeObject(model), token), ex);
                throw ex;
            }
        }
 
        /// <summary>
        /// post请求--传如凭证的方发
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <param name="uri"></param>
        /// <param name="credential"></param>
        /// <param name="content_type"></param>
        /// <returns></returns>
        public static T PostWindowsAuth<T>(object model, string uri, NetworkCredential credential, string content_type = "application/json; charset=utf-8")
        {
            WebClient wc = new WebClient();
            wc.Credentials = credential;
            wc.Headers.Add(HttpRequestHeader.ContentType, content_type);
            byte[] postData = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
            //var content=new FormUrlEncodedContent() .netframwork 4.5
            try
            {
                byte[] text = wc.UploadData(uri, "post", postData);
                string str = System.Text.Encoding.UTF8.GetString(text);
                _log.Info("request:{1}" + JsonConvert.SerializeObject(model) + " visit url:" + uri);
                return JsonConvert.DeserializeObject<T>(str);
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("request:{1}  uri:{0}访问异常  ", uri, JsonConvert.SerializeObject(model)), ex);
                throw ex;
            }
        }


评价
没有个性,不需要签名
文章
6
粉丝
16
评论
8
分类
16
{{item.ArticleTitle}}
{{item.BlogName}} : {{item.Content}}