. Net automatically obtains the token,. net obtains the token,

Source: Internet
Author: User

. Net automatically obtains the token,. net obtains the token,

. NET automatically obtains the Token,

 

 

 

I will directly split it, followed by the complete code, which can be copied to your project,

This method is a bit stupid, but there is no problem

 

 

 

 

 

-------------------------------------------------------------- Copy from the bottom ------------------------------------------------------

 

 

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 the update is correct. // </summary> private static int _ datetime = 0;

/// <Summary> // Access_token, get one time per hour // </summary> private static string Access_token {get {// second Judgment prevents multiple requests 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 () {// the first judgment, refresh the Token every hour, here with the 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 = ""; // url string uri = string. format (@ "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = {0} & secret = {1} ", APPID, Secret );

// Initialize http request HttpWebRequest request = HttpWebRequest. Create (uri) as HttpWebRequest; request. Method = "GET"; // request Method request. AllowAutoRedirect = false; // disable redirection

// Obtain the returned information 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 }}

 

 

 

 

Bytes -------------------------------------------------------------------------------------------------------

 

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.