. NET micro-credit public number development Create custom menu _javascript Tips

Source: Internet
Author: User

I. Preface

Before we developed, we needed to read the official interface documentation and had to spit it out, and this official document was really lame, but we had to look at those documents in order to develop the functionality we needed.

Interface Document Address: http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html

After reading these documents, the basic idea is that we create the menu we want to create, post it to the micro-trust server, and then give us a bit of status code to determine whether our menu was created successfully, just to do some authentication before sending the JSON data.

Two. Preparatory work

First write the menu we want to create in a txt text:

 {'
   button ': [
    {
      ' type ': ' View ',
      ' name ': ' Pay parking fee ',
      ' url ': ' http://www.baidu.com '
  
    },{
      ' Name ': ' Personal center ',
      ' Sub_button ': [
      {  
        ' type ': ' View ',
        ' name ': ' Personal information ',
        ' url ': ' http:// Www.baidu.com "
      },
      {
        " type ":" View ",
        " name ":" Order Query ",
        " url ":" Http://www.baidu.com "
      } ,
      {
        ' type ': ' View ',
        ' name ': ' Use Help ',
        ' url ': ' Http://www.baidu.com '
      },
      {
        ' type ': "View",
        "name": "Download App",
        "url": "Http://www.baidu.com"
      }
    ]
 }

Three. Start coding

First we create a generic handler createmenu.ashx.

Copy Code code as follows:

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);
}

The note here is that appid,secret these parameters need to be replaced by our own, which we can put in the configuration file. It can also be placed separately in a Help class.

At the same time when creating the menu we need to take my access_token this token to verify our identity, then the first thing we need to do is to get our token, how the token to get, we can get through an interface, We just need to pass the two parameters of our AppID and secret.

Copy Code code as follows:

{"Access_token": "jvlat9rp9dngxi4pb4rwlsx_9hjlxicmk_uwdlrtaug8wcawhzz10eqzcyrzrewcijf1-vbhs9yex00dj7q__ Ljcytiwoxtruod25opkf-0 "," expires_in ": 7200}

The return value of the GetPage method above. So we can get our tokens.

The final step: Take our tokens and post our JSON menu data to create the menu.

When you see the following code:

{"Errcode": 0, "errmsg": "OK"}
Shows that your menu was created successfully.

Four: GetPage

The code is as follows:

    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);
        Prepare request ... try {//Set parameter 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 (); Send request and get corresponding response data response = Request.
        GetResponse () as HttpWebResponse; Until request.
The GetResponse () program only starts sending post requests to the destination Web page        Instream = Response.
        GetResponseStream ();
        sr = new StreamReader (instream, encoding); Returns the resulting web page (HTML) code string content = Sr.
        ReadToEnd (); string err = string.
        Empty;
        Response.Write (content);
      return content; The catch (Exception ex) {string err = ex.
        message; return string.
      Empty;

 }
    }

The above is the entire contents of this article, I hope you can enjoy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.