热门关键字:
jquery > jquery教程 > .net > asp.net实现微信公众平台接口的调用

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

9543
作者:管理员
发布时间:2013/8/24 9:21:54
评论数:2
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=323

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


发布方式

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


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


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


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Net;


public partial class weixin : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        string weixin1 = "";
        weixin1 += "{\n";
        weixin1 += "\"button\":[\n";
        weixin1 += "{\n";
        weixin1 += "\"type\":\"click\",\n";
        weixin1 += "\"name\":\"今日歌曲\",\n";
        weixin1 += "\"key\":\"V1001_TODAY_MUSIC123eee\"\n";
        weixin1 += "},\n";
        weixin1 += "{\n";
        weixin1 += "\"type\":\"click\",\n";
        weixin1 += "\"name\":\"歌手简介\",\n";
        weixin1 += "\"key\":\"V1001_TODAY_SINGER123eee\"\n";
        weixin1 += "},\n";
        weixin1 += "{\n";
        weixin1 += "\"name\":\"菜单\",\n";
        weixin1 += "\"sub_button\":[\n";
        weixin1 += "{\n";
        weixin1 += "\"type\":\"click\",\n";
        weixin1 += "\"name\":\"hello word\",\n";
        weixin1 += "\"key\":\"V1001_HELLO_WORLD123eee\"\n";
        weixin1 += "},\n";
        weixin1 += "{\n";
        weixin1 += "\"type\":\"click\",\n";
        weixin1 += "\"name\":\"赞一下我们\",\n";
        weixin1 += "\"key\":\"V1001_GOOD123eee\"\n";
        weixin1 += "}]\n";
        weixin1 += "}]\n";
        weixin1 += "}\n";
        string i = GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=你自己的token", weixin1);

    }
    public string GetPage(string posturl, string postData)
    {
        Stream outstream = null;
        Stream instream = null;
        StreamReader sr = null;
        HttpWebResponse response = null;
        HttpWebRequest request = null;
        Encoding encoding = Encoding.UTF8;
        byte[] data = encoding.GetBytes(postData);
        // 准备请求...
        try
        {
            // 设置参数
            request = WebRequest.Create(posturl) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            outstream = request.GetRequestStream();
            outstream.Write(data, 0, data.Length);
            outstream.Close();
            //发送请求并获取相应回应数据
            response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            instream = response.GetResponseStream();
            sr = new StreamReader(instream, encoding);
            //返回结果网页(html)代码
            string content = sr.ReadToEnd();
            string err = string.Empty;
            return content;
        }
        catch (Exception ex)
        {
            string err = ex.Message;
            return string.Empty;
        }
    }
}


源码打包下载:

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





如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:微信 微信公众平台 微信公众平台接口 asp.net
友荐云推荐