This article mainly introduces the JS-SDK sharing function of the. NET implementation code related information, the need for friends can refer to the following
What is the Js-sdk interface?
In order to facilitate the implementation of the Web page (browser-based Web page) features, such as photo, image selection, voice, location and other mobile phone system capabilities, and to facilitate direct use of the developers to share, sweep and other unique capabilities, the introduction of JS-SDK's overall development package for developers to use.
Share Features
The official documentation provides PHP, Java, node. js, and Python sample code, but there is no C # version, in order to compensate for the needs of the majority of. NET users, I copied the PHP version of the sample code logic into the. NET version, and in the front of the Web page to join the sharing function, I hope to be useful to everyone.
Program implementation
Flow chart
The key class in the program is JSSDK, which contains all the logical processes for server-side request authentication, and the following is a flowchart of the process:
Critical Code Analysis
To ensure the security of data transfer between the third-party server and the server, all interfaces are invoked HTTPS, so the network packets in. NET that refer to the relatively high version (. Net 4.5+) 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;}
Obtain the Access_token, first from the local access_token.aspx, if there is no or expired (7,000 seconds), then back to the server to obtain.
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 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;}