Http请求封装基类HttpHelper.cs
来源:互联网 发布:北京软件外包 编辑:程序博客网 时间:2024/06/05 12:06
HttpHelper请求封装基类,支持get请求和POS请求http接口交互,为后面接口交互做准备。
1、HttpHelper帮助基类
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Net;using System.Net.Security;namespace Weixin.Utils{ public class HttpHelper { public static string Post(string url, string paramData) { return Post(url, paramData, Encoding.UTF8); } public static string Post(string url, string paramData, Encoding encoding) { string result; if (url.ToLower().IndexOf("https", System.StringComparison.Ordinal) > -1) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((sender, certificate, chain, errors) => { return true; }); } try { var wc = new WebClient(); if (string.IsNullOrEmpty(wc.Headers["Content-Type"])) wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); wc.Encoding = encoding; result = wc.UploadString(url, "POST", paramData); } catch (Exception e) { throw; } return result; } public static string Get(string url) { return Get(url, Encoding.UTF8); } public static string Get(string url, Encoding encoding) { try { var wc = new WebClient { Encoding = encoding }; var readStream = wc.OpenRead(url); using (var sr = new StreamReader(readStream, encoding)) { var result = sr.ReadToEnd(); return result; } } catch (Exception e) { throw e; } } }}
2、帮助类使用
var data = "{\"touser\":\"Rich\" },"; data += "{\"msgtype\": \"text\"},"; data += "{\"text\": [{\"content\": \"test\"}]}"; var json = HttpHelper.Post("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken, data);
var result = JsonHelper.Deserialize(json);
1 0
- Http请求封装基类HttpHelper.cs
- HttpHelper.cs类
- HttpHelper封装类
- 【java】自己封装的httpHelper访问类及其相关联的类 访问http
- Java HTTP请求类封装
- Http封装请求工具类
- 封装一个HTTP请求的辅助类
- Android 封装http请求的工具类
- 请求Http链接的封装类
- Android 封装http请求的工具类
- Android 封装http请求的工具类
- HTTP请求封装Java工具类
- php写的http请求封装类
- Android中Http请求类的封装
- Android 封装http请求的工具类
- 安卓Http Post请求封装类
- android 封装Http请求的帮助类
- Android封装Http请求
- Extjs4.0+HSSFWorkbook+SpringMVC实现将数据库中的记录导出到本地Excel格式
- The method of type must override a superclass method解决方式
- android Qemu GPS 模块简明分析
- 使用QT创建系统托盘
- cgpdfpageref转uiimage
- Http请求封装基类HttpHelper.cs
- Android OpenGL ES 简明开发教程七:材质渲染
- 【cocos2dx 3.2】一个都不能死7 游戏结束场景
- 微动力WQ_V3.1020UTF商业版程序,10月最新版完全免费下载
- c++ 显式类型转换static_cast, dynamic_cast, const_cast
- JAVA 根据设置的概率生成随机数
- 水仙花数
- MPEG I B P 帧
- 利用sys.sysprocesses检查SqlServer的阻塞和死锁