微信公众号开发系列-Http请求包基类
2023-09-11 14:21:01 时间
HttpHelper请求包基类,支持get请求和POS要求。以促进微通道交互界面的开发,为了准备的人机交互界面,背部。
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);
相关文章
- http和https的区别,HTTPS工作原理,状态码,cookie和session对于HTTP有什么用
- app中使用微信分享注意事项
- SwipeListView 具体解释 实现微信,QQ等滑动删除效果
- HTTP深入浅出 http请求
- HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot-报错解决方法
- 微信小程序 - 下拉菜单组件
- 微信小程序 - 时间戳转时间
- c# 微信支付的Native扫码支付,下载的“SDK与DEMO”为啥运行报错
- 【小程序】微信小程序button组件无法调整宽度的解决方法(已解决)
- 微信小程序开发:http请求
- 微信小程序 - 网络请求(wx.request)封装:Promise 获取回调结果及降低微信 API 耦合度
- ecshop微信接口基础认识
- 第三方应用分享到微信朋友圈功能
- 微信小程序HTTP封装请求
- 欢迎关注我的微信公众号
- 如何解决微信小程序界面适配问题-引用-生命周期回调函数-优化机制-样式引入
- 微信小程序的wx-charts插件-tab选项卡
- HTTP 响应头信息(Http Response Header) Content-Length 和 Transfer-Encoding
- 微信 小程序组件 电话
- 微信 小程序组件 搜索分类 带缓存(终极 上线版)
- 微信小程序-简易计算器
- Android实现截图分享qq,微信
- 微信小程序自定义组件
- 微信小程序生成二维码、程序码、海报
- Fundebug 微信小游戏异常监控插件更新至 0.5.0,支持监控 HTTP 慢请求
- 微信支付
- Django开发微信公众平台
- 面对微信小程序的威胁,支付宝可以考虑安心做B2C的社交了
- 微信小程序又双叕改获取用户头像昵称的规则了,微信小程序如何去最新获取头像昵称
- http 之 HTTP_X_FORWARDED_FOR