1. public class HttpClientHelper
  2. {
  3. /// <summary>
  4. /// get请求
  5. /// </summary>
  6. /// <param name="url"></param>
  7. /// <returns></returns>
  8. public static string GetResponse(string url)
  9. {
  10. if (url.StartsWith("https"))
  11. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  12.  
  13. HttpClient httpClient = new HttpClient();
  14. httpClient.DefaultRequestHeaders.Accept.Add(
  15. new MediaTypeWithQualityHeaderValue("application/json"));
  16. HttpResponseMessage response = httpClient.GetAsync(url).Result;
  17.  
  18. if (response.IsSuccessStatusCode)
  19. {
  20. string result = response.Content.ReadAsStringAsync().Result;
  21. return result;
  22. }
  23. return null;
  24. }
  25.  
  26. public static T GetResponse<T>(string url)
  27. where T : class,new()
  28. {
  29. if (url.StartsWith("https"))
  30. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  31.  
  32. HttpClient httpClient = new HttpClient();
  33. httpClient.DefaultRequestHeaders.Accept.Add(
  34. new MediaTypeWithQualityHeaderValue("application/json"));
  35. HttpResponseMessage response = httpClient.GetAsync(url).Result;
  36.  
  37. T result = default(T);
  38.  
  39. if (response.IsSuccessStatusCode)
  40. {
  41. Task<string> t = response.Content.ReadAsStringAsync();
  42. string s = t.Result;
  43.  
  44. result = JsonConvert.DeserializeObject<T>(s);
  45. }
  46. return result;
  47. }
  48.  
  49. /// <summary>
  50. /// post请求
  51. /// </summary>
  52. /// <param name="url"></param>
  53. /// <param name="postData">post数据</param>
  54. /// <returns></returns>
  55. public static string PostResponse(string url, string postData)
  56. {
  57. if (url.StartsWith("https"))
  58. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  59.  
  60. HttpContent httpContent = new StringContent(postData);
  61. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  62. HttpClient httpClient = new HttpClient();
  63.  
  64. HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
  65.  
  66. if (response.IsSuccessStatusCode)
  67. {
  68. string result = response.Content.ReadAsStringAsync().Result;
  69. return result;
  70. }
  71. return null;
  72. }
  73.  
  74. /// <summary>
  75. /// 发起post请求
  76. /// </summary>
  77. /// <typeparam name="T"></typeparam>
  78. /// <param name="url">url</param>
  79. /// <param name="postData">post数据</param>
  80. /// <returns></returns>
  81. public static T PostResponse<T>(string url, string postData)
  82. where T : class,new()
  83. {
  84. if (url.StartsWith("https"))
  85. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  86.  
  87. HttpContent httpContent = new StringContent(postData);
  88. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  89. HttpClient httpClient = new HttpClient();
  90.  
  91. T result = default(T);
  92.  
  93. HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
  94.  
  95. if (response.IsSuccessStatusCode)
  96. {
  97. Task<string> t = response.Content.ReadAsStringAsync();
  98. string s = t.Result;
  99.  
  100. result = JsonConvert.DeserializeObject<T>(s);
  101. }
  102. return result;
  103. }
  104.  
  105. /// <summary>
  106. /// V3接口全部为Xml形式,故有此方法
  107. /// </summary>
  108. /// <typeparam name="T"></typeparam>
  109. /// <param name="url"></param>
  110. /// <param name="xmlString"></param>
  111. /// <returns></returns>
  112. public static T PostXmlResponse<T>(string url, string xmlString)
  113. where T : class,new()
  114. {
  115. if (url.StartsWith("https"))
  116. System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
  117.  
  118. HttpContent httpContent = new StringContent(xmlString);
  119. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  120. HttpClient httpClient = new HttpClient();
  121.  
  122. T result = default(T);
  123.  
  124. HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
  125.  
  126. if (response.IsSuccessStatusCode)
  127. {
  128. Task<string> t = response.Content.ReadAsStringAsync();
  129. string s = t.Result;
  130.  
  131. result = XmlDeserialize<T>(s);
  132. }
  133. return result;
  134. }
  135.  
  136. /// <summary>
  137. /// 反序列化Xml
  138. /// </summary>
  139. /// <typeparam name="T"></typeparam>
  140. /// <param name="xmlString"></param>
  141. /// <returns></returns>
  142. public static T XmlDeserialize<T>(string xmlString)
  143. where T : class,new ()
  144. {
  145. try
  146. {
  147. XmlSerializer ser = new XmlSerializer(typeof(T));
  148. using (StringReader reader = new StringReader(xmlString))
  149. {
  150. return (T)ser.Deserialize(reader);
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. throw new Exception("XmlDeserialize发生异常:xmlString:" + xmlString + "异常信息:" + ex.Message);
  156. }
  157.  
  158. }
  159. }

  

转载地址:http://www.2cto.com/weixin/201501/367405.html

C# HttpClientHelper请求的更多相关文章

  1. .Net之简单通知服务

    开篇语 这两天看见有大佬分享使用钉钉和企业微信的机器人来做通知报警,然后我想到了我使用的另一个第三方软件捷易快信(可能大家都不知道这个东西,我也忘了我最开始是咋知道的),该服务的优点是可以通过微信进行 ...

  2. C#微信开发之旅(二):基础类之HttpClientHelper(更新:SSL安全策略)

    public class HttpClientHelper   2     {   3         /// <summary>   4         /// get请求   5    ...

  3. Android实现异步处理 -- HTTP请求

    原帖:http://www.cnblogs.com/answer1991/archive/2012/04/22/2464524.html Android操作UI的方法不是线程安全的,也就是说开发者自己 ...

  4. commons-httpclient 实现get和post请求

    引入的jar包为: <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> &l ...

  5. java 常见几种发送http请求案例

    import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...

  6. Java Http 请求

    package zr.weixin.com.utils; import java.io.BufferedReader; import java.io.IOException; import java. ...

  7. c# Http请求之HttpClient

    利用HttpClient进行Http请求,基于此,简单地封装了下: using System; using System.Collections.Generic; using System.Colle ...

  8. java Http post请求发送json字符串

    最近差点被业务逻辑搞懵逼,果然要先花时间思考,确定好流程再执行.目前最好用的jar包还是org.apache.http. public class HttpClientHelper { private ...

  9. httpcomponents 发送get post请求

    引入的包为: <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <de ...

随机推荐

  1. ajaxfileupload异步上传附件添加參数的方法

    1.js文件 // JavaScript Document jQuery.extend({ createUploadIframe: function(id, uri) { //create frame ...

  2. 山寨一个std::bind\boost::bind

    这里是最初始的版本,参考https://github.com/cplusplus-study/fork_stl/blob/master/include/bind.hpp 提供了最简洁的实现方式. 第一 ...

  3. linux命令(31):more

    一.more命令 more功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 ...

  4. 深入理解Eureka - Eureka配置列表

    Eureka包含四个部分的配置 instance:当前Eureka Instance实例信息配置 client:Eureka Client客户端特性配置 server:Eureka Server注册中 ...

  5. eclipse的remote search

    一般你希望以部分文件名作为关键字的时候,可以选择这个搜索选项,当然你也可以选择操作系统自带的搜索功能

  6. LeetCode: Regular Expression Matching 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

  7. Linux maven 下 jar包下载不下来的解决方法

    在项目中遇到一个问题,使用推送的中间件,结果使用maven下载不下来,当时就直接手动下载后,copy到本地仓库了, 结果发布的时候遇到问题了. 因为发布是直接在测试服务器上,将提交的svn代码进行打包 ...

  8. Android笔记(十一)第一个Fragment

    Fragment是碎片的意思,能够參照Activity来理解Fragment,由于它们都能包括布局,都有自己的生命周期. 以下我们要让主活动包括两个碎片,而且让两个碎片充满屏幕 1.首先,新建两个碎片 ...

  9. plot sin 04 坐标轴居中

    plot sin 04 坐标轴居中 code #!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import matpl ...

  10. 模仿Struts2的Interceptor拦截器实现

    模仿Struts2的Interceptor拦截器实现 public interface Invocation { public Object invoke(); } public interface ...