
- 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;
- }
- }
评价
排名
72
文章
8
粉丝
3
评论
3
导出SQL脚本小程序
剑轩 : 厉害了!
导出SQL脚本小程序
剑轩 : 厉害了!
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术