c# 微信分享-原始代碼


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

第一步:

  我在后置文件里面創建這么幾個參數,WxPayApi是引用的微信官方demo里面的WxPayApi,

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


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 = "";

 

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

//分享代碼
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"];

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

 

//加密代碼

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM