php专区

 首页 > php专区 > PHP应用 > 开放平台 > C#微信开发之旅(二):基础类之HttpClientHelper(更新

C#微信开发之旅(二):基础类之HttpClientHelper(更新

分享到:
【字体:
导读:
          包含通过HttpClient发起get或post请求的方法,所有调用微信接口的操作都通过此类。话不多说,直接上代码:2014-10-31代码更新:微信SSL安全策略调整,关闭掉SSLv2、SSLv3版本支持,不再支持...

包含通过HttpClient发起get或post请求的方法,所有调用微信接口的操作都通过此类。话不多说,直接上代码:

2014-10-31代码更新:微信SSL安全策略调整,关闭掉SSLv2、SSLv3版本支持,不再支持部分使用SSLv2、 SSLv3或更低版本的客户端调用。

 public class HttpClientHelper
  2     {
  3         /// 
  4         /// get请求
  5         /// 
  6         /// 
  7         /// 
  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(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 t = response.Content.ReadAsStringAsync();
 42                 string s = t.Result;
 43 
 44                 result = JsonConvert.DeserializeObject(s);
 45             }
 46             return result;
 47         }
 48 
 49         /// 
 50         /// post请求
 51         /// 
 52         /// 
 53         /// post数据
 54         /// 
 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         /// 
 75         /// 发起post请求
 76         /// 
 77         /// 
 78         /// url
 79         /// post数据
 80         /// 
 81         public static T PostResponse(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 t = response.Content.ReadAsStringAsync();
 98                 string s = t.Result;
 99 
100                 result = JsonConvert.DeserializeObject(s);
101             }
102             return result;
103         }
104 
105         /// 
106         /// V3接口全部为Xml形式,故有此方法
107         /// 
108         /// 
109         /// 
110         /// 
111         /// 
112         public static T PostXmlResponse(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 t = response.Content.ReadAsStringAsync();
129                 string s = t.Result;
130 
131                 result = XmlDeserialize(s);
132             }
133             return result;
134         }
135 
136         /// 
137         /// 反序列化Xml
138         /// 
139         /// 
140         /// 
141         /// 
142         public static T XmlDeserialize(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     }

分享到:
C#微信开发之旅(十二):V2告警接口&...
告警接口,是微信用来通知我们一样警告信息,可以存储到DB定期查看并解决;维权则是用户主动发起的,需要我们处理。。。。 1      /// 2 /// 维权通知 3 /// 4 /// 5 public void Notice() 6 { 7 #region 用户新增维权 8 9 ...
C#微信开发之旅(十一):V2发货接口 - 微...
用户支付完成后,V2版本微信支付需要调用发货接口,否则微信会告警并且用户也可以进行维权,总之会有灰常多的事情: 1 public void DeliverNotify() 2 { 3 string openId = string.Empty; //买家openid 4 string transactionId = string.Empty;//微信交易单号 5 ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……