首页 技术 正文
技术 2022年11月15日
0 收藏 391 点赞 3,414 浏览 1672 个字

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

1、HttpHelper帮助基类

[csharp] view
plain
copy

  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. namespace Weixin.Utils
  9. {
  10. public class HttpHelper
  11. {
  12. public static string Post(string url, string paramData)
  13. {
  14. return Post(url, paramData, Encoding.UTF8);
  15. }
  16. public static string Post(string url, string paramData, Encoding encoding)
  17. {
  18. string result;
  19. if (url.ToLower().IndexOf(“https”, System.StringComparison.Ordinal) > -1)
  20. {
  21. ServicePointManager.ServerCertificateValidationCallback =
  22. new RemoteCertificateValidationCallback((sender, certificate, chain, errors) => { return true; });
  23. }
  24. try
  25. {
  26. var wc = new WebClient();
  27. if (string.IsNullOrEmpty(wc.Headers[“Content-Type”]))
  28. wc.Headers.Add(“Content-Type”, “application/x-www-form-urlencoded”);
  29. wc.Encoding = encoding;
  30. result = wc.UploadString(url, “POST”, paramData);
  31. }
  32. catch (Exception e)
  33. {
  34. throw;
  35. }
  36. return result;
  37. }
  38. public static string Get(string url)
  39. {
  40. return Get(url, Encoding.UTF8);
  41. }
  42. public static string Get(string url, Encoding encoding)
  43. {
  44. try
  45. {
  46. var wc = new WebClient { Encoding = encoding };
  47. var readStream = wc.OpenRead(url);
  48. using (var sr = new StreamReader(readStream, encoding))
  49. {
  50. var result = sr.ReadToEnd();
  51. return result;
  52. }
  53. }
  54. catch (Exception e)
  55. {
  56. throw e;
  57. }
  58. }
  59. }
  60. }

2、帮助类使用

[csharp] view
plain
copy

  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

  1. var result = JsonHelper.Deserialize(json);

本人新浪微博:http://weibo.com/i/1741159542

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919