• WeChatUtil类


    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Text;
    
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
        public class WeChatUtil
        {
            /// <summary>
            /// 获取TOKEN
            /// </summary>
            /// <param name="strCorpid">企业号标识</param>
            /// <param name="strSecret">微信获取token地址</param>
            /// <param name="strSecret">具体应用Secret</param>
            /// <param name="strWeChatServiceUrl">微信接口处理地址</param>
            /// <returns></returns>
            public string GetAccessToken(string strCorpid, string strSecret, string accesstokenurl, string strWeChatServiceUrl)
            {
                string strAccessToken = string.Empty;  
                string strGetTokenUrl = strWeChatServiceUrl + "?action=gettoken";
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("corpid", strCorpid);
                dic.Add("corpsecret", strSecret);
                HttpRequestUtil httpUtil = new HttpRequestUtil();
                strAccessToken = httpUtil.SendHttpGetRequest(strGetTokenUrl, dic);
                if (!string.IsNullOrEmpty(strAccessToken))
                {
                    return strAccessToken;
                }
    
                //重新获取AccessToken
                
                HttpRequestUtil util = new HttpRequestUtil();
                string strResult = util.SendHttpGetRequest(accesstokenurl, dic);
                //解析出token  
                //{"access_token":"DGubVxT5W-SaeYgGjJA6anTzkl6XnlP-WlsWpoKcUNGkha5ZI6ePHRefmTlG7-IB","expires_in":7200}
                JObject jobject = JsonConvert.DeserializeObject<JObject>(strResult);
                strAccessToken = jobject["access_token"].ToString();
                string strExpire = jobject["expires_in"].ToString();
    
                //更新数据库
                string strUpdateTokenUrl = strWeChatServiceUrl;
                Dictionary<string, string> dicUpdateToken = new Dictionary<string, string>();
                dic.Add("action", "updatetoken");
                dic.Add("token", strAccessToken);//传递四个参数过去
                dic.Add("expires_in", strExpire);
                strResult = httpUtil.SendHttpGetRequest(strUpdateTokenUrl, dic);
                return strAccessToken;
            }
        }
    © 版权声明 文章版权归作者所有,若需转载,请在显著位置标志该文章地址。
  • 相关阅读:
    mybatis <=或这个>=提示错误Tag name expecte问题解决
    Navicat 设置自增长初始值
    mysql 时间与字符串相互转换
    jquery 动态控制显隐
    mysql 查询常见时间段数据
    jquery 取得select选中的值
    java8 运算语法集
    springboot 单元测试
    idea 自动生成并跳转单元测试
    限制页面被pc端访问
  • 原文地址:https://www.cnblogs.com/luchenglong/p/13667808.html
Copyright © 2020-2023  润新知