1.微信小程序的必备Model
public class WxConfig { /// <summary> /// 小程序的appId /// 登录小程序可以直接看到 /// </summary> public string AppId { get; set; } /// <summary> /// 小程序的AppSecret /// 只能修改,不能查看 /// </summary> public string AppSecret { get; set; } /// <summary> /// 商户key:微信商户平台(pay.weixin.qq.com)-->账户设置-->API安全-->密钥设置(这个key是自己定义的。32位数字加字母组成的) /// </summary> public string PayKey { get; set; } /// <summary> /// 商户号 /// 支付时需要 /// </summary> public string MchId { get; set; } /// <summary> /// 微信的token 用过的都知道是干嘛的 /// </summary> public string Access_token { get; set; } /// <summary> /// 最新获取token的时间.token的时效只有7200秒 /// </summary> public DateTime UpTokenTime { get; set; } }
2.后台Get请求方法和POST请求方法
/// <summary> /// GET请求方式 /// </summary> /// <param name="postUrl"></param> /// <returns></returns> public static string GetWebRequest(string postUrl) { var ret = string.Empty; try { var request = WebRequest.Create(postUrl); var response = request.GetResponse(); var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); ret = reader.ReadToEnd(); reader.Close(); reader.Dispose(); response.Close(); } catch (Exception ex) { return ex.Message; } return ret; } /// <summary> /// Post获取数据流 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> public static Stream PostMoths(string url, string data) { HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; //string paraUrlCoded = param; byte[] payload; //payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); payload = System.Text.Encoding.UTF8.GetBytes(data); request.ContentLength = payload.Length; Stream writer = request.GetRequestStream(); writer.Write(payload, 0, payload.Length); writer.Close(); System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); //System.IO.Stream s; return response.GetResponseStream(); }
3.备注:小程序有些api的接口都只能在后台进行请求。不要在前台进行请求.合理利用:app.js的globalData对象
请发表评论