|
实现的功能:
1:获取用户的基本信息,
2:获取用户的详细资料
3:实现了微信扫描二维码的功能
大体就做了上面3个功能,其余的没有怎么做。
web.config可以配置微星的appid和secret,还有刷新token时间,默认为1小时刷新一次。
- <!--微信开发-->
- <add key="weixin_appid" value="xxx" />
- <add key="weixin_secret" value="xxx" />
- <add key="weixin_sleep" value="3600000" />
- <!--结束-->
复制代码
下面是获取token的代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using CsharpHttpHelper;
- using System.Net;
- using Newtonsoft.Json;
- using System.Threading;
- namespace WeChat.BLL
- {
- /// <summary>
- /// 微信token
- /// </summary>
- public class WeChatToken
- {
- #region 获取token
- /// <summary>
- /// 获取token
- /// </summary>
- /// <returns></returns>
- public static string GetToken()
- {
- HttpHelper http = new HttpHelper();
- HttpItem item = new HttpItem
- {
- URL = string.Format(Config.URL_GET_TOKEN, Config.APPID, Config.SECRET),
- Method = "get",
- Encoding = Encoding.UTF8
- };
- HttpResult result = http.GetHtml(item);
- if (result.StatusCode == HttpStatusCode.OK)
- {
- string html = result.Html;
- Models.success success = JsonConvert.DeserializeObject<Models.success>(html);
- return success.access_token;
- }
- return null;
- }
- /// <summary>
- /// 获取token
- /// </summary>
- /// <param name="appid"></param>
- /// <param name="secret"></param>
- /// <returns></returns>
- public static string GetToken(string appid, string secret)
- {
- Config.APPID = appid;
- Config.SECRET = secret;
- return GetToken();
- }
- #endregion
- #region 获取js token
- /// <summary>
- /// js sdk token
- /// </summary>
- /// <returns></returns>
- public static string JsToken()
- {
- HttpHelper http = new HttpHelper();
- HttpItem item = new HttpItem
- {
- URL = string.Format(Config.URL_JS_TOKEN, Config.TOKEN),
- Method = "get",
- Encoding = Encoding.UTF8
- };
- HttpResult result = http.GetHtml(item);
- if (result.StatusCode == HttpStatusCode.OK)
- {
- string html = result.Html;
- Models.JsToken jstoken = JsonConvert.DeserializeObject<Models.JsToken>(html);
- return jstoken.ticket;
- }
- return null;
- }
- #endregion
- #region 获取token任务
- /// <summary>
- /// 获取token任务
- /// </summary>
- /// <param name="token"></param>
- public static void TaskToken(task_token token)
- {
- Config.APPID = token.appid;
- Config.SECRET = token.secret;
- while (true)
- {
- //token带重试机制
- for (int i = 0; i < 3; i++)
- {
- string get_token = GetToken();
- if (!string.IsNullOrEmpty(get_token))
- {
- Config.TOKEN = get_token;
- break;
- }
- }
- //js token带重试机制
- for (int i = 0; i < 3; i++)
- {
- string js_token = JsToken();
- if (!string.IsNullOrEmpty(js_token))
- {
- Config.JSAPI_TICKET = js_token;
- break;
- }
- }
- Thread.Sleep(token.time);
- }
- }
- /// <summary>
- /// 获取token任务
- /// </summary>
- /// <param name="appid"></param>
- /// <param name="secret"></param>
- /// <param name="time"></param>
- public static void TaskToken(string appid, string secret, int time = 3600000)
- {
- Config.APPID = appid;
- Config.SECRET = secret;
- while (true)
- {
- Config.TOKEN = GetToken();
- Thread.Sleep(time);
- }
- }
- #endregion
- }
- }
复制代码
其实,一些基本的功能都很简单,在做微信扫一扫功能的时候,花了一小时才完成。
如果源码缺少HttpHelper.dll,请下载:
HttpHelper.dll
(28.5 KB, 下载次数: 95)
源码下载:
https://down.itsvse.com/item/9.html
|
上一篇:webapi接收中文乱码的问题下一篇:.net IQueryable与IList差别之处
|