微信公众号开发系列-Http请求封装基类

HttpHelper请求封装基类,支持get请求和POS请求,方便微信开发接口交互,为后面接口交互做准备。

1、HttpHelper帮助基类

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Net;  
  7. using System.Net.Security;  
  8.   
  9. namespace Weixin.Utils  
  10. {  
  11.     public class HttpHelper  
  12.     {  
  13.         public static string Post(string url, string paramData)  
  14.         {  
  15.             return Post(url, paramData, Encoding.UTF8);  
  16.         }  
  17.   
  18.         public static string Post(string url, string paramData, Encoding encoding)  
  19.         {  
  20.             string result;  
  21.   
  22.             if (url.ToLower().IndexOf("https", System.StringComparison.Ordinal) > -1)  
  23.             {  
  24.                 ServicePointManager.ServerCertificateValidationCallback =  
  25.                                new RemoteCertificateValidationCallback((sender, certificate, chain, errors) => { return true; });  
  26.             }  
  27.   
  28.             try  
  29.             {  
  30.                 var wc = new WebClient();  
  31.                 if (string.IsNullOrEmpty(wc.Headers["Content-Type"]))  
  32.                     wc.Headers.Add("Content-Type""application/x-www-form-urlencoded");  
  33.                 wc.Encoding = encoding;  
  34.   
  35.                 result = wc.UploadString(url, "POST", paramData);  
  36.             }  
  37.             catch (Exception e)  
  38.             {  
  39.                 throw;  
  40.             }  
  41.   
  42.             return result;  
  43.         }  
  44.   
  45.         public static string Get(string url)  
  46.         {  
  47.             return Get(url, Encoding.UTF8);  
  48.         }  
  49.   
  50.         public static string Get(string url, Encoding encoding)  
  51.         {  
  52.             try  
  53.             {  
  54.                 var wc = new WebClient { Encoding = encoding };  
  55.                 var readStream = wc.OpenRead(url);  
  56.                 using (var sr = new StreamReader(readStream, encoding))  
  57.                 {  
  58.                     var result = sr.ReadToEnd();  
  59.                     return result;  
  60.                 }  
  61.             }  
  62.             catch (Exception e)  
  63.             {  
  64.                 throw e;  
  65.             }  
  66.         }  
  67.     }  
  68. }  

2、帮助类使用

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var data = "{\"touser\":\"Rich\" },";  
  2.         data += "{\"msgtype\": \"text\"},";  
  3.         data += "{\"text\": [{\"content\": \"test\"}]}";  
  4.         var json = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken, data);  
[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. var result = JsonHelper.Deserialize(json);  

本人新浪微博:http://weibo.com/i/1741159542
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值