Home > Article > Backend Development > .Net implements WeChat JS-SDK sharing function code display
This article mainly introduces the relevant information about the .Net implementation code of the WeChat JS-SDK sharing function. Friends in need can refer to it
What is the JS-SDK interface?
In order to facilitate developers to implement webpage functions in WeChat (webpages accessed based on WeChat browser), such as the capabilities of mobile phone systems such as taking pictures, selecting pictures, voice, and location, and to facilitate developers to directly use WeChat to share and scan. With WeChat's unique capabilities such as scanning, WeChat has launched an overall development package of JS-SDK for developers to use conveniently.
Sharing function
The official documentation provides sample codes for php, java, node.js and python, but there is no c# version. In order to make up for the vast number of .net To meet the needs of users, I copied the sample code logic of the PHP version into the .net version, and added a sharing function to the web front-end. I hope it will be useful to everyone.
Program implementation
Flow chart
The key class in the program is JSSDK, which contains all the server-side request authentication Logical process, the following is the flow chart of the process:
Key code analysis
To ensure the connection between the third-party server and the WeChat server For the security of data transmission, all WeChat interfaces are called using https, so .net references a higher version (.Net 4.5+) network package for 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 search it from the local access_token.aspx. If it does not exist or expires (7000 seconds), go to the WeChat server again 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, the principle is the same 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; }
The above is the detailed content of .Net implements WeChat JS-SDK sharing function code display. For more information, please follow other related articles on the PHP Chinese website!