Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A tutorial on the method of .net implementation code of Wechat JS-SDK sharing function

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "Wechat JS-SDK sharing function .net code implementation method tutorial". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is the JS-SDK interface?

To make it convenient for developers to achieve the functions of web pages (web pages accessed based on Wechat browser) in Wechat, such as the ability to take pictures, select pictures, voice, location and other mobile systems, and to make it convenient for developers to directly use Wechat's unique capabilities such as Wechat sharing and scanning, Wechat has launched an overall development package of JS-SDK for developers' convenience.

Sharing function

The sample code of php, java, node.js and python is provided in the official document, but not the c # version. In order to make up for the needs of. Net users, I copied the php version of the sample code logic into. Net version, and added the sharing function in the front end of the web page, hoping to be useful to everyone.

Program realization

Flow chart

The key class in the program is JSSDK, which contains all the logical processes of the server requesting authentication. The following is the flow chart of the process:

Key code analysis

In order to ensure the security of data transmission between the third-party server and Wechat server, all Wechat interfaces are called by https, so the higher version (.net 4.5 +) network packets are referenced in .net to make http requests.

Private string httpGet (string url) {if (url.StartsWith ("https")) System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; HttpClient httpClient = new HttpClient (); httpClient.DefaultRequestHeaders.Accept.Add (new MediaTypeWithQualityHeaderValue ("application/json")); HttpResponseMessage response = httpClient.GetAsync (url) .Result; if (response.IsSuccessStatusCode) {string result = response.Content.ReadAsStringAsync (). Result; return result;} return null;}

To obtain the access_token, first look for it from the local access_token.aspx, and if it does not exist or expires (7000 seconds), then go back to the Wechat server to obtain it.

Private string getAccessToken () {string accessToken = string.Empty; var data = JObject.Parse (getAspxFile ("access_token.aspx", ASPX_ head [1])); if (data! = null & & long.Parse (data ["expire_time"]. ToString () < Utils.ConvertTimeStamp (DateTime.Now)) {string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + this.appId +" & secret= "+ this.appSecret Var jRes = JObject.Parse (httpGet (url)); accessToken = jRes ["access_token"]. ToString (); if (! string.IsNullOrEmpty (accessToken)) {data ["expire_time"] = Utils.ConvertTimeStamp (new DateTime ()) + 7000; data ["access_token"] = accessToken; setAspxFile ("access_token.aspx", data.ToString (), ASPX_HEAD [1]) }} else accessToken = data ["access_token"] .ToString (); return accessToken;}

Get jsapi_ticket on the same principle as access_token.

Private string getJsApiTicket () {string ticket = string.Empty; var data = JObject.Parse (getAspxFile ("jsapi_ticket.aspx", ASPX_ head [0])); if (data! = null & & long.Parse (data ["expire_time"]. ToString ()) < Utils.ConvertTimeStamp (DateTime.Now)) {string accessToken = getAccessToken (); string url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" + accessToken Var jRes = JObject.Parse (httpGet (url)); ticket = jRes ["ticket"]. ToString (); if (! string.IsNullOrEmpty (ticket)) {data ["expire_time"] = Utils.ConvertTimeStamp (new DateTime ()) + 7000; data ["jsapi_ticket"] = ticket; setAspxFile ("jsapi_ticket.aspx", data.ToString (), ASPX_HEAD [0]) }} else ticket = data ["jsapi_ticket"]. ToString (); return ticket;} "Wechat JS-SDK sharing function of the .net implementation of the code method tutorial" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report