Home  >  Article  >  Database  >  微信公共平台开发-(.net实现)5--access

微信公共平台开发-(.net实现)5--access

WBOY
WBOYOriginal
2016-06-07 15:44:171236browse

每次在于微信交互时,都要用到access_token,但是这个值限制的是有时间的,但是access_token,在以后的高级功能里面会经常用到,所以这里不得不这里对前面所讲解的access_token改造一下。 另外需要说明的是access_token是变化的,有自己的周期,官方解释为:"

    每次在于微信交互时,都要用到access_token,但是这个值限制的是有时间的,但是access_token,在以后的高级功能里面会经常用到,所以这里不得不这里对前面所讲解的access_token改造一下。
   另外需要说明的是access_token是变化的,有自己的周期,官方解释为:"有效期为7200秒",这就要求我们把获得的access_token存入一个物理文件或者Application中,请求到过期后修改这些内容,需要用的时候读出来.或者是存入数据库,到期时修改改access_token的值。
   有些人可能想到了,如果过期我就在获得一个就好了,不用物理文件和Application也可以达到同样的效果,但是需要注意了微信平台对每天获得,access_token的次数也作了限制,一个用户出发多次,如果用户多,那肯定就超出了。所以我们还是按照以上的思路实现这些功能:
    在此之前我们已经了解了获得access_token的方法(连接)http://www.cnblogs.com/QLJ1314/p/3838058.html,现在只需要保证它的随时更新就好了.

      首先建立一个Access_token类

  

微信公共平台开发-(.net实现)5--access微信公共平台开发-(.net实现)5--access

<span> 1</span>     <span>///</span> <span><summary></summary></span>  
<span> 2</span>     <span>///</span><span>Access_token 的摘要说明  
</span><span> 3</span>     <span>///</span> <span></span>  
<span> 4</span>     <span>public</span> <span>class</span><span> Access_token  
</span><span> 5</span> <span>    {  
</span><span> 6</span>         <span>public</span><span> Access_token()  
</span><span> 7</span> <span>        {  
</span><span> 8</span>             <span>//</span>  
<span> 9</span>             <span>//</span><span>TODO: 在此处添加构造函数逻辑  
</span><span>10</span>             <span>//</span>  
<span>11</span> <span>        }  
</span><span>12</span>         <span>string</span><span> _access_token;  
</span><span>13</span>         <span>string</span><span> _expires_in;  
</span><span>14</span>       
<span>15</span>         <span>///</span> <span><summary></summary></span>  
<span>16</span>         <span>///</span><span> 获取到的凭证   
</span><span>17</span>         <span>///</span> <span></span>  
<span>18</span>         <span>public</span> <span>string</span><span> access_token  
</span><span>19</span> <span>        {  
</span><span>20</span>             <span>get</span> { <span>return</span><span> _access_token; }  
</span><span>21</span>             <span>set</span> { _access_token =<span> value; }  
</span><span>22</span> <span>        }  
</span><span>23</span>       
<span>24</span>         <span>///</span> <span><summary></summary></span>  
<span>25</span>         <span>///</span><span> 凭证有效时间,单位:秒  
</span><span>26</span>         <span>///</span> <span></span>  
<span>27</span>         <span>public</span> <span>string</span><span> expires_in  
</span><span>28</span> <span>        {  
</span><span>29</span>             <span>get</span> { <span>return</span><span> _expires_in; }  
</span><span>30</span>             <span>set</span> { _expires_in =<span> value; }  
</span><span>31</span> <span>        }  
</span><span>32</span>     }  
View Code

微信公共平台开发-(.net实现)5--access微信公共平台开发-(.net实现)5--access

<span>1</span>     <?xml version=<span>"<span>1.0</span><span>"</span> encoding=<span>"</span><span>utf-8</span><span>"</span>?>  
<span>2</span>     <xml>  
<span>3</span>       <access_token>初始值可以随便写</access_token>  
<span>4</span>       <access_youxrq><span>1990</span>/<span>12</span>/<span>12</span> <span>16</span>:<span>06</span>:<span>38</span></access_youxrq>  
<span>5</span>     </xml>  
View Code

之前获得Access_token的方法改造一下,让他给Access_token实例赋值

微信公共平台开发-(.net实现)5--access微信公共平台开发-(.net实现)5--access

<span> 1</span>     <span>public</span> <span>static</span><span> Access_token GetAccess_token()  
</span><span> 2</span> <span>        {  
</span><span> 3</span>             <span>string</span> appid =<span> 你的appid ;  
</span><span> 4</span>             <span>string</span> secret =<span> 你的secret;  
</span><span> 5</span>             <span>string</span> strUrl = <span>"</span><span>https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=</span><span>"</span> + appid + <span>"</span><span>&secret=</span><span>"</span> +<span> secret;  
</span><span> 6</span>             Access_token mode = <span>new</span><span> Access_token();  
</span><span> 7</span>       
<span> 8</span>             HttpWebRequest req =<span> (HttpWebRequest)HttpWebRequest.Create(strUrl);  
</span><span> 9</span>       
<span>10</span>             req.Method = <span>"</span><span>GET</span><span>"</span><span>;  
</span><span>11</span>             <span>using</span> (WebResponse wr =<span> req.GetResponse())  
</span><span>12</span> <span>            {  
</span><span>13</span>                 HttpWebResponse myResponse =<span> (HttpWebResponse)req.GetResponse();  
</span><span>14</span>       
<span>15</span>                 StreamReader reader = <span>new</span><span> StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);  
</span><span>16</span>       
<span>17</span>                 <span>string</span> content =<span> reader.ReadToEnd();  
</span><span>18</span>                 <span>//</span><span>Response.Write(content);  
</span><span>19</span>                 <span>//</span><span>在这里对Access_token 赋值  </span>
<span>20</span>                 Access_token token = <span>new</span><span> Access_token();  
</span><span>21</span>                 token = JsonHelper.ParseFromJson<access_token><span>(content);  
</span><span>22</span>                 mode.access_token =<span> token.access_token;  
</span><span>23</span>                 mode.expires_in =<span> token.expires_in;  
</span><span>24</span> <span>            }  
</span><span>25</span>             <span>return</span><span> mode;  
</span><span>26</span>         }  </access_token>
View Code

以上的方法用到了Json对象的处理,所以我把JsonHelper的代码一贴出来供大家参考,一下就是JsonHelper.cs的代码:

微信公共平台开发-(.net实现)5--access微信公共平台开发-(.net实现)5--access

<span> 1</span>     <span>using</span><span> System;  
</span><span> 2</span>     <span>using</span><span> System.IO;  
</span><span> 3</span>     <span>using</span><span> System.Text;  
</span><span> 4</span>     <span>using</span><span> System.Runtime.Serialization.Json;  
</span><span> 5</span>        
<span> 6</span>        
<span> 7</span>       
<span> 8</span>     <span>public</span> <span>class</span><span> JsonHelper  
</span><span> 9</span> <span>    {  
</span><span>10</span>         <span>///</span> <span><summary></summary></span>  
<span>11</span>         <span>///</span><span> 生成Json格式  
</span><span>12</span>         <span>///</span> <span></span>  
<span>13</span>         <span>///</span> <span><typeparam name="T"></typeparam></span>  
<span>14</span>         <span>///</span> <span><param name="obj"></span>  
<span>15</span>         <span>///</span> <span><returns></returns></span>  
<span>16</span>         <span>public</span> <span>static</span> <span>string</span> GetJson<t><span>(T obj)  
</span><span>17</span> <span>        {  
</span><span>18</span>             DataContractJsonSerializer json = <span>new</span><span> DataContractJsonSerializer(obj.GetType());  
</span><span>19</span>             <span>using</span> (MemoryStream stream = <span>new</span><span> MemoryStream())  
</span><span>20</span> <span>            {  
</span><span>21</span> <span>                json.WriteObject(stream, obj);  
</span><span>22</span>                 <span>string</span> szJson = Encoding.UTF8.GetString(stream.ToArray()); <span>return</span><span> szJson;  
</span><span>23</span> <span>            }  
</span><span>24</span> <span>        }  
</span><span>25</span>         <span>///</span> <span><summary></summary></span>  
<span>26</span>         <span>///</span><span> 获取Json的Model  
</span><span>27</span>         <span>///</span> <span></span>  
<span>28</span>         <span>///</span> <span><typeparam name="T"></typeparam></span>  
<span>29</span>         <span>///</span> <span><param name="szJson"></span>  
<span>30</span>         <span>///</span> <span><returns></returns></span>  
<span>31</span>         <span>public</span> <span>static</span> T ParseFromJson<t>(<span>string</span><span> szJson)  
</span><span>32</span> <span>        {  
</span><span>33</span>             T obj = Activator.CreateInstance<t><span>();  
</span><span>34</span>             <span>using</span> (MemoryStream ms = <span>new</span><span> MemoryStream(Encoding.UTF8.GetBytes(szJson)))  
</span><span>35</span> <span>            {  
</span><span>36</span>                 DataContractJsonSerializer serializer = <span>new</span><span> DataContractJsonSerializer(obj.GetType());  
</span><span>37</span>                 <span>return</span><span> (T)serializer.ReadObject(ms);  
</span><span>38</span> <span>            }  
</span><span>39</span> <span>        }  
</span><span>40</span>     }  </t></t></t>
View Code

其实还可以直接饮用Newtonsoft.Json.dll 可以直接操作json,这样更方便一些。

我们还需要写一个判断access_token是否过期如果过期更新XML文件的方法。

微信公共平台开发-(.net实现)5--access微信公共平台开发-(.net实现)5--access

<span> 1</span>     <span>///</span> <span><summary></summary></span>  
<span> 2</span>         <span>///</span><span> 根据当前日期 判断Access_Token 是否超期  如果超期返回新的Access_Token   否则返回之前的Access_Token  
</span><span> 3</span>         <span>///</span> <span></span>  
<span> 4</span>         <span>///</span> <span><param name="datetime"></span>  
<span> 5</span>         <span>///</span> <span><returns></returns></span>  
<span> 6</span>         <span>public</span> <span>static</span> <span>string</span><span> IsExistAccess_Token()  
</span><span> 7</span> <span>        {  
</span><span> 8</span>       
<span> 9</span>             <span>string</span> Token = <span>string</span><span>.Empty;  
</span><span>10</span> <span>            DateTime YouXRQ;  
</span><span>11</span>             <span>//</span><span> 读取XML文件中的数据,并显示出来 ,注意文件路径  </span>
<span>12</span>             <span>string</span> filepath = Server.MapPath(<span>"</span><span>XMLFile.xml</span><span>"</span><span>);  
</span><span>13</span>       
<span>14</span>             StreamReader str = <span>new</span><span> StreamReader(filepath, System.Text.Encoding.UTF8);  
</span><span>15</span>             XmlDocument xml = <span>new</span><span> XmlDocument();  
</span><span>16</span> <span>            xml.Load(str);  
</span><span>17</span> <span>            str.Close();  
</span><span>18</span> <span>            str.Dispose();  
</span><span>19</span>             Token = xml.SelectSingleNode(<span>"</span><span>xml</span><span>"</span>).SelectSingleNode(<span>"</span><span>Access_Token</span><span>"</span><span>).InnerText;  
</span><span>20</span>             YouXRQ = Convert.ToDateTime(xml.SelectSingleNode(<span>"</span><span>xml</span><span>"</span>).SelectSingleNode(<span>"</span><span>Access_YouXRQ</span><span>"</span><span>).InnerText);  
</span><span>21</span>       
<span>22</span>             <span>if</span> (DateTime.Now ><span> YouXRQ)  
</span><span>23</span> <span>            {  
</span><span>24</span>                 DateTime _youxrq =<span> DateTime.Now;  
</span><span>25</span>                 Access_token mode =<span> GetAccess_token();  
</span><span>26</span>                 xml.SelectSingleNode(<span>"</span><span>xml</span><span>"</span>).SelectSingleNode(<span>"</span><span>Access_Token</span><span>"</span>).InnerText =<span> mode.access_token;  
</span><span>27</span>                 _youxrq = _youxrq.AddSeconds(<span>int</span><span>.Parse(mode.expires_in));  
</span><span>28</span>                 xml.SelectSingleNode(<span>"</span><span>xml</span><span>"</span>).SelectSingleNode(<span>"</span><span>Access_YouXRQ</span><span>"</span>).InnerText =<span> _youxrq.ToString();  
</span><span>29</span> <span>                xml.Save(filepath);  
</span><span>30</span>                 Token =<span> mode.access_token;  
</span><span>31</span> <span>            }  
</span><span>32</span>             <span>return</span><span> Token;  
</span><span>33</span>         }  
View Code

好了,完成了上面的工作,我只需要在使用到access_token的时调用如下就OK了,“客户再也不用担心token的过期”
string _access_token = IsExistAccess_Token();

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn