国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home WeChat Applet WeChat Development Use .NET account to develop WeChat public account and create custom menu method

Use .NET account to develop WeChat public account and create custom menu method

Mar 14, 2017 pm 03:06 PM

這篇文章利用.NET號開發(fā)微信公眾號創(chuàng)建自定義菜單方法的相關(guān)資料,需要的朋友可以參考下

一.前言

開發(fā)之前,我們需要閱讀官方的接口說明文檔,不得不吐槽一下,微信的這個(gè)官方文檔真的很爛,但是,為了開發(fā)我們需要的功能,我們也不得不去看這些文檔.

接口文檔地址:http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html

看了這些個(gè)文檔,基本意思明白了,就是我們把我們要?jiǎng)?chuàng)建的菜單創(chuàng)建好,post到微信的服務(wù)器上面,微信服務(wù)器然后給我們一些狀態(tài)碼,從而判斷我們的菜單是否創(chuàng)建成功,只是在發(fā)送json數(shù)據(jù)以前我們要做一些身份驗(yàn)證。

二.準(zhǔn)備工作

首先把我們要?jiǎng)?chuàng)建的菜單寫在一個(gè)txt文本中:


        public string  access_token { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            FileStream fs1 = new FileStream(Server.MapPath(".") + "\\menu.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("UTF-8"));
            string menu = sr.ReadToEnd();
            sr.Close();
            fs1.Close();
            var str = GetPage("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd811f5114e3e56f3&secret=76eb33f66129692da16d148cb3c024f1", "");
            JObject jo = JObject.Parse(str);
            access_token = jo["access_token"].ToString();
            GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token + "", menu);
        }


三.開始編碼

首先我們創(chuàng)建一個(gè)一般處理程序createMenu.ashx.

代碼如下:

        public string  access_token { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            FileStream fs1 = new FileStream(Server.MapPath(".") + "\\menu.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding("UTF-8"));
            string menu = sr.ReadToEnd();
            sr.Close();
            fs1.Close();
            var str = GetPage("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd811f5114e3e56f3&secret=76eb33f66129692da16d148cb3c024f1", "");
            JObject jo = JObject.Parse(str);
            access_token = jo["access_token"].ToString();
            GetPage("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token + "", menu);
        }

  這里需要注意的是appid,secret這些參數(shù)需要換成我們自己的,這些參數(shù)我們可以放在配置文件中。也可以單獨(dú)的放在一個(gè)幫助類里面。

同時(shí)在創(chuàng)建菜單的時(shí)候我們需要帶上我的access_token這個(gè)令牌來驗(yàn)證我們的身份,那么我們首先要做的就是獲取我們的這個(gè)令牌,那個(gè)這個(gè)令牌要如何獲取了,我們可以通過一個(gè)接口獲取,只需要傳遞我們的appid和secret這個(gè)兩個(gè)參數(shù)


代碼如下:

{"access_token":"jVLAT9Rp9dNgxI4pb4RWlSx_9HJLXICmk_uWDlRtAug8wcaWhZZ10eqZCYRZrEwCIJf1-vBhS9YEX00Dj7qlJCyTIWOxTruOd25opkf-0","expires_in":7200}

  上面的GetPage方法的返回值。這樣我們就可以獲取我們的令牌了。

最后一步:帶上我們的令牌,post我們的json菜單數(shù)據(jù)就可以創(chuàng)建菜單了。

當(dāng)你看到如下代碼:

{"errcode":0,"errmsg":"ok"}
  說明你的菜單創(chuàng)建成功了。

四:GetPage

代碼如下:


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);
  // 準(zhǔn)備請求...
  try
  {
    // 設(shè)置參數(shù)
    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();
    //發(fā)送請求并獲取相應(yīng)回應(yīng)數(shù)據(jù)
    response = request.GetResponse() as HttpWebResponse;
    //直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁發(fā)送Post請求
    instream = response.GetResponseStream();
    sr = new StreamReader(instream, encoding);
    //返回結(jié)果網(wǎng)頁(html)代碼
    string content = sr.ReadToEnd();
    string err = string.Empty;
    Response.Write(content);
    return content;
  }
  catch (Exception ex)
  {
    string err = ex.Message;
    return string.Empty;
  }
}


以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡

The above is the detailed content of Use .NET account to develop WeChat public account and create custom menu method. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276