. NET automatically acquires tokens,

Source: Internet
Author: User

. NET to get tokens automatically,

I'll just slice it, and the whole code behind it can be copied to your project,

I'm a little stupid, but I don't have any problems.

--------------------------------------------------------------Copy from below---------------------------------------------------- --

Using System; Using System.Collections.Generic; Using System.IO; Using System.Linq; Using System.Net; Using System.Text; Using System.Web; Using System.Web.Script.Serialization; Using Wechat.models;

Namespace Wechat.dal {

public class Wechatcommon {#region get Access_token

<summary>//Token//</summary> private static string _access_token {get; set; }///<summary>///time, used to determine whether or not to update the///</summary> private static int _datetime = 0;

       //<summary>        // Access_token, get once per hour        //</summary>          private static string Access_token         {             Get              {               // Second judgment prevents multiple applications                 if (_ DateTime! = DateTime.Now.Hour)                  {                     _datetime = DateTime.Now.Hour;                     if (Getaccess_token ())                          return _access_ Token                      Else                          return null;                }                  Else                      return _access_token;            }        }         #endregion

        #region Get Access_token        // <summary>        //return Access_token         //</summary>        //<returns></returns>          private static string Returnaccess_token ()          {           //first time judgment, token is refreshed once per hour, with lock              if (_datetime! = DateTime.Now.Hour)              {                 Lock (Access_token)                  {                    return access_token;                }             }              return _access_token;

}

<summary>//Get Getaccess_token//</summary>//<returns>Access_token<             /returns> private static bool Getaccess_token () {String Secret = "";             String APPID = ""; The URL string Uri =string. Format (@ "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", Appid,secret);

           //initializing HTTP requests              HttpWebRequest request = Httpwebrequest.create (URI) as HttpWebRequest;             request. method = "GET";//Request Methods             requests. AllowAutoRedirect = false;//Prohibit redirection

           //Get the information returned              using (WebResponse web = Request. GetResponse ())             {                 StreamReader reader = new StreamReader (web. GetResponseStream (), Encoding.UTF8);                 string json = reader. ReadToEnd ();                 JavaScriptSerializer JS = new JavaScriptSerializer ();                 Access_token Access_ token = js. Deserialize<access_token> (JSON);

                if (access_ Token.errcode! = "41004" && access_token.errcode! = null)                  {                     _access_token = Access_token.access_token;                      return true;                }                  Else                      return false;

}         }

#endregion}}

-------------------------------------------------------------------------------------------------------

. NET automatically acquires tokens,

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.