您的位置:首页 > 编程语言 > ASP

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

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

发布方式

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

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

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

view
source

print?

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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: