asp.net实现微信公众平台接口的调用

微信公众平台是腾讯公司在微信的基础上新增的功能模块,通过这一平台,个人和企业都可以打造一个微信的公众号,并实现和特定群体的文字、图片、语音的全方位沟通、互动。


发布方式

它的最重要的发布和订阅方式,都在设置中会找到一个如下格式的二维码,品牌 ID 放到二维码的中部。 你也可以有其他方式来订阅微信公众帐号。比如,通过微信号进行订阅,在微信上直接点按“添加朋友”——“按号码查找”,输入“帐号(字母,数字,下划数组合且必须以字母开头)”就可以查找并关注您感兴趣的内容。但是对于开放申请的微信公众号,大部分微信号格式类似wangluowenzhai这样的英文数字结合组成的微信号,并不利于记忆和查找(此外,微信上面还可以通过发送名片的方式把你喜欢的微信公众帐号 ID 发送给朋友)。


最近jquery学堂很多网友问,有没有关于微信公众平台接口的调用的例子啊,没有网友们分享过,只能直接回答没有啦,今天无意中发现了一个asp.net的例子,于是就把它整理出来分享。


接下来教大家怎么用asp.net实现微信公众平台接口的调用,直接贴源码:


01 using System;
02 using System.Collections;
03 using System.Configuration;
04 using System.Data;
05 using System.Linq;
06 using System.Web;
07 using System.Web.Security;
08 using System.Web.UI;
09 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13 using System.Xml;
14 using System.IO;
15 using System.Text;
16 using System.Collections.Generic;
17 using System.Net;
18  
19  
20 public partial class weixin : System.Web.UI.Page
21 {
22  
23  
24     protected void Page_Load(object sender, EventArgs e)
25     {
26         string weixin1 = "";
27         weixin1 += "{\n";
28         weixin1 += "\"button\":[\n";
29         weixin1 += "{\n";
30         weixin1 += "\"type\":\"click\",\n";
31         weixin1 += "\"name\":\"今日歌曲\",\n";
32         weixin1 += "\"key\":\"V1001_TODAY_MUSIC123eee\"\n";
33         weixin1 += "},\n";
34         weixin1 += "{\n";
35         weixin1 += "\"type\":\"click\",\n";
36         weixin1 += "\"name\":\"歌手简介\",\n";
37         weixin1 += "\"key\":\"V1001_TODAY_SINGER123eee\"\n";
38         weixin1 += "},\n";
39         weixin1 += "{\n";
40         weixin1 += "\"name\":\"菜单\",\n";
41         weixin1 += "\"sub_button\":[\n";
42         weixin1 += "{\n";
43         weixin1 += "\"type\":\"click\",\n";
44         weixin1 += "\"name\":\"hello word\",\n";
45         weixin1 += "\"key\":\"V1001_HELLO_WORLD123eee\"\n";
46         weixin1 += "},\n";
47         weixin1 += "{\n";
48         weixin1 += "\"type\":\"click\",\n";
49         weixin1 += "\"name\":\"赞一下我们\",\n";
50         weixin1 += "\"key\":\"V1001_GOOD123eee\"\n";
51         weixin1 += "}]\n";
52         weixin1 += "}]\n";
53         weixin1 += "}\n";
54         string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=你自己的token", weixin1);
55  
56     }
57     public string GetPage(string posturl, string postData)
58     {
59         Stream outstream = null;
60         Stream instream = null;
61         StreamReader sr = null;
62         HttpWebResponse response = null;
63         HttpWebRequest request = null;
64         Encoding encoding = Encoding.UTF8;
65         byte[] data = encoding.GetBytes(postData);
66         // 准备请求...
67         try
68         {
69             // 设置参数
70             request = WebRequest.Create(posturl) as HttpWebRequest;
71             CookieContainer cookieContainer = new CookieContainer();
72             request.CookieContainer = cookieContainer;
73             request.AllowAutoRedirect = true;
74             request.Method = "POST";
75             request.ContentType = "application/x-www-form-urlencoded";
76             request.ContentLength = data.Length;
77             outstream = request.GetRequestStream();
78             outstream.Write(data, 0, data.Length);
79             outstream.Close();
80             //发送请求并获取相应回应数据
81             response = request.GetResponse() as HttpWebResponse;
82             //直到request.GetResponse()程序才开始向目标网页发送Post请求
83             instream = response.GetResponseStream();
84             sr = new StreamReader(instream, encoding);
85             //返回结果网页(html)代码
86             string content = sr.ReadToEnd();
87             string err = string.Empty;
88             return content;
89         }
90         catch (Exception ex)
91         {
92             string err = ex.Message;
93             return string.Empty;
94         }
95     }
96 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值