The. Net implementation code of wechat js-sdk sharing function

Time:2021-4-28

What is the js-sdk interface?

In order to facilitate developers to realize the functions of wechat web pages (web pages accessed based on wechat browser), such as the ability to take photos, select pictures, voice, location and other mobile phone systems, and to facilitate developers to directly use the unique capabilities of wechat such as wechat sharing and scanning, wechat launched the overall development package of js-sdk for developers to use conveniently.

Sharing function

Official documents provide PHP, Java, node.js and python sample code, but there is no C # version. In order to make up for the needs of the majority of. Net users, I copy the example code logic of PHP version to. Net version, and add the sharing function in the front end of the web page, hoping to be useful to you.

Program implementation

flow chart

The key class in the program is jssdk, which contains all the logical processes for the server to request authentication

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 in the form of HTTPS, so. Net refers to the network package of higher version (. Net 4.5 +) for HTTP request.


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;
}

Get access_ Token, first from the local access_ Token.aspx. If it is not available or expired (7000 seconds), go to wechat server again.


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, principle and access_ The same as 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;
}

Complete code

https://github.com/stozen/jssdk-wxshare

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.