zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    © 版权声明 文章版权归作者所有,若需转载,请在显著位置标志该文章地址。
  • 相关阅读:
    到具体某一天的倒计时
    angular2 2种方式----获取子组件的类属性和类方法
    页面刷新
    angular父子组件传值
    div垂直居中,文字垂直居中!!!
    Python 基础数据类型 II (列表)
    Python 基础数据类型 I (str等)
    学习笔记411
    20190407 Word合并单元格
    VBA正则笔记 理解肯定环视
  • 原文地址:https://www.cnblogs.com/luchenglong/p/13667808.html
Copyright © 2011-2022 走看看