HttpClient的帮助类

  1. public class HttpHelper
  2. {
  3. /// <summary>
  4. /// 发起POST同步请求
  5. /// </summary>
  6. /// <param name="url"></param>
  7. /// <param name="postData"></param>
  8. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  9. /// <param name="headers">填充消息头</param>
  10. /// <returns></returns>
  11. public static string HttpPost(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
  12. {
  13. postData = postData ?? "";
  14. using (HttpClient client = new HttpClient())
  15. {
  16. if (headers != null)
  17. {
  18. foreach (var header in headers)
  19. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  20. }
  21. using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
  22. {
  23. if (contentType != null)
  24. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  25.  
  26. HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
  27. return response.Content.ReadAsStringAsync().Result;
  28. }
  29. }
  30. }
  31.  
  32. /// <summary>
  33. /// 发起POST异步请求
  34. /// </summary>
  35. /// <param name="url"></param>
  36. /// <param name="postData"></param>
  37. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  38. /// <param name="headers">填充消息头</param>
  39. /// <returns></returns>
  40. public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
  41. {
  42. postData = postData ?? "";
  43. using (HttpClient client = new HttpClient())
  44. {
  45. client.Timeout = new TimeSpan(0, 0, timeOut);
  46. if (headers != null)
  47. {
  48. foreach (var header in headers)
  49. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  50. }
  51. using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
  52. {
  53. if (contentType != null)
  54. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  55.  
  56. HttpResponseMessage response = await client.PostAsync(url, httpContent);
  57. return await response.Content.ReadAsStringAsync();
  58. }
  59. }
  60. }
  61.  
  62. /// <summary>
  63. /// 发起GET同步请求
  64. /// </summary>
  65. /// <param name="url"></param>
  66. /// <param name="headers"></param>
  67. /// <param name="contentType"></param>
  68. /// <returns></returns>
  69. public static string HttpGet(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
  70. {
  71. using (HttpClient client = new HttpClient())
  72. {
  73. if (contentType != null)
  74. client.DefaultRequestHeaders.Add("ContentType", contentType);
  75. if (headers != null)
  76. {
  77. foreach (var header in headers)
  78. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  79. }
  80. HttpResponseMessage response = client.GetAsync(url).Result;
  81. return response.Content.ReadAsStringAsync().Result;
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// 发起GET异步请求
  87. /// </summary>
  88. /// <param name="url"></param>
  89. /// <param name="headers"></param>
  90. /// <param name="contentType"></param>
  91. /// <returns></returns>
  92. public static async Task<string> HttpGetAsync(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
  93. {
  94. using (HttpClient client = new HttpClient())
  95. {
  96. if (contentType != null)
  97. client.DefaultRequestHeaders.Add("ContentType", contentType);
  98. if (headers != null)
  99. {
  100. foreach (var header in headers)
  101. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  102. }
  103. HttpResponseMessage response = await client.GetAsync(url);
  104. return await response.Content.ReadAsStringAsync();
  105. }
  106. }
  107.  
  108. /// <summary>
  109. /// 发起POST同步请求
  110. /// </summary>
  111. /// <param name="url"></param>
  112. /// <param name="postData"></param>
  113. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  114. /// <param name="headers">填充消息头</param>
  115. /// <returns></returns>
  116. public static T HttpPost<T>(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
  117. {
  118. return HttpPost(url, postData, contentType, timeOut, headers).ToEntity<T>();
  119. }
  120.  
  121. /// <summary>
  122. /// 发起POST异步请求
  123. /// </summary>
  124. /// <param name="url"></param>
  125. /// <param name="postData"></param>
  126. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  127. /// <param name="headers">填充消息头</param>
  128. /// <returns></returns>
  129. public static async Task<T> HttpPostAsync<T>(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
  130. {
  131. var res = await HttpPostAsync(url, postData, contentType, timeOut, headers);
  132. return res.ToEntity<T>();
  133. }
  134.  
  135. /// <summary>
  136. /// 发起GET同步请求
  137. /// </summary>
  138. /// <param name="url"></param>
  139. /// <param name="headers"></param>
  140. /// <param name="contentType"></param>
  141. /// <returns></returns>
  142. public static T HttpGet<T>(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
  143. {
  144. return HttpGet(url, contentType, headers).ToEntity<T>();
  145. }
  146.  
  147. /// <summary>
  148. /// 发起GET异步请求
  149. /// </summary>
  150. /// <param name="url"></param>
  151. /// <param name="headers"></param>
  152. /// <param name="contentType"></param>
  153. /// <returns></returns>
  154. public static async Task<T> HttpGetAsync<T>(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
  155. {
  156. var res = await HttpGetAsync(url, contentType, headers);
  157. return res.ToEntity<T>();
  158. }
  159. }
  160. /// <summary>
  161. /// Json扩展方法
  162. /// </summary>
  163. public static class JsonExtends
  164. {
  165. public static T ToEntity<T>(this string val)
  166. {
  167. return JsonConvert.DeserializeObject<T>(val);
  168. }
  169. public static string ToJson<T>(this T entity, Formatting formatting = Formatting.None)
  170. {
  171. return JsonConvert.SerializeObject(entity, formatting);
  172. }
  173. }

SqlServer 常用语句方法的更多相关文章

  1. SqlServer常用语句整理

    先记录下来 以后整理 1.常用语句 1.1update连表更新 update a set a.YCaseNo = a.WordName + '['+ convert(varchar,a.CaseYea ...

  2. SqlServer常用语句

    首先,写这个的原因是我其实sql语句不太行,总觉得自己写得很乱,好像也没有系统学习过,借此复习和与大家探讨 No.1 关于查询时间区间是否重叠的sql语句 问题是这样:插入之前,想查询同User是否其 ...

  3. SqlServer常用语句参考

    1.SQL SERVER中如何使用SQL 语句复制表结构: select * into 新表 from 旧表 where 1=2 把“旧表”的表结构复制到“新表”,1=2不复制数据,如果要复制数据,就 ...

  4. C#学习笔记(4)——sqlserver常用语句

    说明(2017-5-26 17:29:05): 需要天天练习: 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] ...

  5. sqlserver 常用语句

    1.查询表中的RID RID=RowID=(fileID:pageID:slotID) SELECT sys.fn_PhysLocFormatter(%%physloc%%) AS rid,* FRO ...

  6. MySQL 常用语句大全

    MySQL 常用语句大全 一.连接 MySQL 格式: mysql -h 主机地址 -u 用户名 -p 用户密码 1.例 1:连接到本机上的 MYSQL. 首先在打开 DOS 窗口,然后进入目录 my ...

  7. SQLServer查询语句收集

    常用的SQLServer查询语句,有空可以多练习一下,增加记忆,可以提高工作效率! 1.数据操作 Select      --从数据库表中检索数据行和列Insert      --向数据库表添加新数据 ...

  8. SQLServer查询语句收集(非常实用)

    =============================    SQLServer语句收集1  =========================== 1.数据操作  Select      --从 ...

  9. SQL server 常用语句

    SQL Server中常用的SQL语句   1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10. ...

随机推荐

  1. C语言中typedef用法

    C语言中typedef用法 1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等) ...

  2. 尝试在阿里云的Linux服务器器上安装拥有图形界面的Pycharm

    在Linux服务器上跑Python项目发现每次从本地上传文件太过麻烦,于是打算在服务器上安装Pycharm直接写Pycharm代码.   去Pycharm的官网下载Linux版本(支持正版于是我下载了 ...

  3. ubuntu+mysql+php+apache2+wordpress建站全记录

    虽然操作并不难,但用到的各种命令,各种坑的解决方法还需要记一下 建好的博客: 念诗之人的博客 VPS和域名选购 VPS选购 国内外有很多商家可供选择,国内有如阿里云,百度云,腾讯云等(ECS,BCC等 ...

  4. Bayesian Non-Exhaustive Classification A case study:online name disambiguation using temporal record streams

    一 摘要: name entity disambiguation:将对应多个人的记录进行分组,使得每个组的记录对应一个人. 现有的方法多为批处理方式,需要将所有的记录输入给算法. 现实环境需要1:以o ...

  5. HDU_4570_区间dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4570 连题目都看不懂,直接找了题解,copy了过来= =. 一个长度为n的数列,将其分成若干段(每一段的长度要& ...

  6. yum 升级php版本

    centos默认安装的php都是 5.3的  ,现在需要 5.6以上的版本 手动安装比较麻烦,直接用yum升级了. 一.准备工作 首先检查当前php版本 #php -v 查看安装的php扩展包 #yu ...

  7. linux web站点常用压力测试工具httperf

    一.工具下载&&安装 软件获取 ftp://ftp.hpl.hp.com/pub/httperf/ 这里使用的是如下的版本 ftp://ftp.hpl.hp.com/pub/httpe ...

  8. iptables服务器主机防火墙

    iptables参数说明: Commands: Either long or short options are allowed. --append -A chain 链尾部追加一条规则 --dele ...

  9. webpack性能优化

    Webpack优化打包速度以及性能优化 1.跟上技术的迭代(Node.Npm.Yarn) 2.在尽可能少的模块上应用loader 3.Plugin尽可能精简并确保可靠 4.resolve参数合理配置 ...

  10. vue路由--动态路由

    前面介绍的路由都是路径和组件一对一映射的 有时候需要多个路径映射到一个组件,这个组件根据参数的不同动态改变,这时候需要用到动态路由 动态路由这样定义路由路径: path: '/foo/:id'--可以 ...