首页 > 网站 > WEB开发 > 正文

微信 JSSDK .NET版

2024-04-27 14:14:05
字体:
来源:转载
供稿:网友
微信 JSSDK .NET版
/*因为官方 微信 JSSDK 只有php java版本的 我自己照着PHP的翻译过来的,可供参考。欢迎指正*/  [csharp] view plaincopy在CODE上查看代码片派生到我的代码片using System;  using System.Collections.Generic;  using System.Web;  using System.IO;  using System.Security.Cryptography;      using System.Runtime.Serialization.Json;  using System.Runtime.Serialization;  using System.Net;  using System.Runtime.Serialization.Formatters.Binary;  using System.Text;      namespace JSSDK.Models  {      public class JSSDK      {          PRivate string appId;          private string appSecret;              public JSSDK(string appId, string appSecret)          {              this.appId = appId;              this.appSecret = appSecret;          }              //创建Json序列化 及反序列化类目          #region          //创建JSon类 保存文件 jsapi_ticket.json          [DataContract(Namespace = "http://coderzh.cnblogs.com")]          class JSTicket          {              [DataMember(Order = 0)]              public string jsapi_ticket { get; set; }              [DataMember(Order = 1)]              public double expire_time { get; set; }          }          //创建 JSon类 保存文件 access_token.json          [DataContract(Namespace = "http://coderzh.cnblogs.com")]          class AccToken          {              [DataMember(Order = 0)]              public string access_token { get; set; }              [DataMember(Order = 1)]              public double expire_time { get; set; }          }              //创建从微信返回结果的一个类 用于获取ticket          [DataContract(Namespace = "http://coderzh.cnblogs.com")]          class Jsapi          {              [DataMember(Order = 0)]              public int errcode { get; set; }              [DataMember(Order = 1)]              public string errmsg { get; set; }              [DataMember(Order = 2)]              public string ticket { get; set; }              [DataMember(Order = 3)]              public string expires_in { get; set; }          }          #endregion              //得到数据包,返回使用页面          public System.Collections.Hashtable getSignPackage()          {                               string jsapiTicket = getJsApiTicket();              string url = HttpContext.Current.Request.Url.ToString(); //"http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";              string timestamp = Convert.ToString(ConvertDateTimeInt(DateTime.Now));              string nonceStr = createNonceStr();                  // 这里参数的顺序要按照 key 值 ASCII 码升序排序              string rawstring = "jsapi_ticket=" + jsapiTicket + "&noncestr=" + nonceStr + "&timestamp=" + timestamp + "&url=" + url + "";                  string signature = SHA1_Hash(rawstring);                  System.Collections.Hashtable signPackage = new System.Collections.Hashtable();              signPackage.Add("appId", appId);              signPackage.Add("nonceStr", nonceStr);              signPackage.Add("timestamp", timestamp);              signPackage.Add("url", url);              signPackage.Add("signature", signature);              signPackage.Add("rawString", rawstring);                  return signPackage;          }              //创建随机字符串          private string createNonceStr()          {                  int length = 16;              string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";              string str = "";              Random rad = new Random();              for (int i = 0; i < length; i++)              {                  str += chars.Substring(rad.Next(0, chars.Length - 1), 1);              }              return str;          }              //得到ticket 如果文件里时间 超时则重新获取          private string getJsApiTicket()          {              // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例              string path =HttpContext.Current.Server.MapPath(@"/jssdk/jsapi_ticket.json");                            //读取文件                     FileStream file = new FileStream(path, FileMode.Open);              var serializer = new DataContractJsonSerializer(typeof(JSTicket));              JSTicket readJSTicket = (JSTicket)serializer.ReadObject(file);              file.Close();              string ticket = "";              if (readJSTicket.expire_time < ConvertDateTimeInt(DateTime.Now))              {                  string accessToken = getAccessToken();                  string url = "https://api.weixin.QQ.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" + accessToken + "";                  Jsapi api = Desrialize<Jsapi>(new Jsapi(), httpGet(url));                          ticket = api.ticket;                  if (ticket != "")                  {                      readJSTicket.expire_time = ConvertDateTimeInt(DateTime.Now) + 7000;                      readJSTicket.jsapi_ticket = ticket;                          string json = Serialize<JSTicket>(readJSTicket);                      StreamWriterMetod(json, path);                  }              }              else              {                  ticket = readJSTicket.jsapi_ticket;              }                  return ticket;          }              //得到accesstoken 如果文件里时间 超时则重新获取          private string getAccessToken()          {              // access_token 应该全局存储与更新,以下代码以写入到文件中做示例                  string access_token = "";              string path = HttpContext.Current.Server.MapPath(@"/jssdk/access_token.json");                         FileStream file = new FileStream(path, FileMode.Open);              var serializer = new DataContractJsonSerializer(typeof(AccToken));              AccToken readJSTicket = (AccToken)serializer.ReadObject(file);              file.Close();              if (readJSTicket.expire_time < ConvertDateTimeInt(DateTime.Now))              {                  string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret + "";                      AccToken iden = Desrialize<AccToken>(new AccToken(), httpGet(url));                      access_token = iden.access_token;                  if (access_token != "")                  {                      iden.expire_time = ConvertDateTimeInt(DateTime.Now) + 7000;                      iden.access_token = access_token;                          string json = Serialize<AccToken>(iden);                      StreamWriterMetod(json, path);                  }              }              else              {                  access_token = readJSTicket.access_token;              }              return access_token;          }              //发起一个http请球,返回值          private string httpGet(string url)          {                 try {              WebClient MyWebClient = new WebClient();                        MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据              Byte[] pageData = MyWebClient.DownloadData(url); //从指定网站下载数据              string pageHtml = System.Text.Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312,则使用这句                          //string pageHtml = Encoding.UTF8.GetString(pageData); //如果获取网站页面采用的是UTF-8,则使用这句              return pageHtml;             }          catch(WebException webEx) {              Console.WriteLine(webEx.Message.ToString());          return null;      }          }              //SHA1哈希加密算法          public string SHA1_Hash(string str_sha1_in)          {              SHA1 sha1 = new SHA1CryptoServiceProvider();              byte[] bytes_sha1_in = System.Text.UTF8Encoding.Default.GetBytes(str_sha1_in);              byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);              string str_sha1_out = BitConverter.ToString(bytes_sha1_out);              str_sha1_out = str_sha1_out.Replace("-", "").ToLower();              return str_sha1_out;          }              /// <summary>          /// StreamWriter写入文件方法          /// </summary>          private void StreamWriterMetod(string str, string patch)          {              try              {                  FileStream fsFile = new FileStream(patch, FileMode.OpenOrCreate);                  StreamWriter swWriter = new StreamWriter(fsFile);                  //寫入數據                  swWriter.WriteLine(str);                  swWriter.Clo
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表