zoukankan      html  css  js  c++  java
  • 微信公众号开发系列-Http请求包基类

    HttpHelper请求包基类,支持get请求和POS要求。以促进微通道交互界面的开发,为了准备的人机交互界面,背部。


    1、HttpHelper帮助基类

    [csharp] view plaincopy在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 plaincopy在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 plaincopy在CODE上查看代码片派生到我的代码片
    1. var result = JsonHelper.Deserialize(json);  

    本人新浪微博:http://weibo.com/i/1741159542
  • 相关阅读:
    笔记20200430:异常机制
    笔记20200429:面向对象
    笔记20200429:数组
    笔记20200428:方法的定义及调用、重装、命令行参数、可变参数、递归
    笔记20200427:javadoc文档、Scanner、顺序结构、选择结构、循环结构
    CSS+DIV入门第一天基础视频 CSS选择器层叠性和继承性
    网站实战 从效果图开始CSS+DIV 布局华为网站
    CSS中的间距设置与盒子模型
    CSS选择器
    CSS颜色及文本字体
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4561760.html
Copyright © 2011-2022 走看看