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

      protected static readonly log4net.ILog _log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// 
        /// get请求
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T Get(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(str);
            }
            catch (Exception ex)
            {
                _log.Error(string.Format(" uri:{0}访问异常  ", uri), ex);
                throw ex;
            }
        }

        /// 
        /// post请求
        /// 
        /// 返回的对象
        /// 请求的对象
        /// 接口地址
        /// token没有可以去掉
        /// 
        /// 
        public static T Post(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(str);
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("request:{1} token:{2} uri:{0}访问异常  ", uri, JsonConvert.SerializeObject(model), token), ex);
                throw ex;
            }
        }

        /// 
        /// post请求--传如凭证的方发
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static T PostWindowsAuth(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(str);
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("request:{1}  uri:{0}访问异常  ", uri, JsonConvert.SerializeObject(model)), ex);
                throw ex;
            }
        }

 

你可能感兴趣的:(.net,webAPI,.net,传入连接凭证调用api接口,post调用接口,get调用接口,调用api接口)