c# 微信分享-原始代碼

==如下僅供新學習的朋友參考,老鳥就不用看了==php

第一步:api

  我在後置文件裏面建立這麼幾個參數,WxPayApi是引用的微信官方demo裏面的WxPayApi,微信

  demo地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1app


public string AppId = "---------------------------";
public string AppSecret = "------------------------------------";
public string timestamp = WxPayApi.GenerateTimeStamp();
public string nonceStr = WxPayApi.GenerateNonceStr();
public string signature = "";
public string link = "";
public string imgUrl = "";
public string desc = "";ide

 

第二步:寫分享代碼 這個建議放在讀取頁面內容的後面學習

//分享代碼
link = Request.Url.ToString();  //獲取當前URL
imgUrl = "http://m.fdc0737.com/images/fdc0737Logo.jpg";  //分享的小圖片
desc = Content;  //分享的描述
string oneUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ AppId + "&secret="+ AppSecret;  //第一次請求文件換access_token 
string result1 = HttpService.Get(oneUrl);
JsonData jd = JsonMapper.ToObject(result1);
string access_token = (string)jd["access_token"];加密

string twoUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token + "&type=jsapi";   //第二次請求微信換ticket 
string result2 = HttpService.Get(twoUrl);
JsonData jd2 = JsonMapper.ToObject(result2);
string ticket = (string)jd2["ticket"];
//string expires_in = (string)jd2["expires_in"];url

string String1 = "jsapi_ticket="+ ticket + "&noncestr="+ nonceStr + "&timestamp="+ timestamp + "&url="+ link;  //拼接字符串準備生成簽名
signature = SHA1(String1, Encoding.UTF8); //微信簽名code

 

//加密代碼token

public static string SHA1(string content, Encoding encode) { SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] bytes_in = encode.GetBytes(content); byte[] bytes_out = sha1.ComputeHash(bytes_in); sha1.Dispose(); string result = BitConverter.ToString(bytes_out); result = result.Replace("-", ""); return result; }

相關文章
相關標籤/搜索