呼び出し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;
            }
        }