.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!

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!