zoukankan      html  css  js  c++  java
  • 微信公共服务平台开发(.Net 的实现)2-------获得ACCESSTOKEN

    成为了开发者之后微信平台会给您appid和secret,在订阅号中是没有的,所以因该申请一下服务号

    有了ACCESSTOKEN才能做添加菜单,上传/下载图片等功能

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. private string GetToken()  
    2.      {  
    3.   
    4.          // 也可以这样写:  
    5.          //return  GetPage("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret", "");  
    6.          
    7.          string res = "";  
    8.          HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential  
    9. &appid=你的appid&secret=你的secret");  
    10.  req.Method = "GET";  
    11.          using (WebResponse wr = req.GetResponse())  
    12.          {  
    13.              HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();  
    14.   
    15.   
    16.              StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);  
    17.   
    18.   
    19.              string content = reader.ReadToEnd();  
    20.      
    21.              List<ACCESSTOKEN> myACCESSTOKEN = Json.JSONStringToList<ACCESSTOKEN>(content);  
    22.              res = myACCESSTOKEN[0].access_token;  
    23.    
    24.          }  
    25.   
    26.   
    27.          return res;  
    28.      }  
    29.      public string GetPage(string posturl, string postData)  
    30.      {  
    31.          Stream outstream = null;  
    32.          Stream instream = null;  
    33.          StreamReader sr = null;  
    34.          HttpWebResponse response = null;  
    35.          HttpWebRequest request = null;  
    36.          Encoding encoding = Encoding.UTF8;  
    37.          byte[] data = encoding.GetBytes(postData);  
    38.          // 准备请求...  
    39.          try  
    40.          {  
    41.              // 设置参数  
    42.              request = WebRequest.Create(posturl) as HttpWebRequest;  
    43.              CookieContainer cookieContainer = new CookieContainer();  
    44.              request.CookieContainer = cookieContainer;  
    45.              request.AllowAutoRedirect = true;  
    46.              request.Method = "POST";  
    47.              request.ContentType = "application/x-www-form-urlencoded";  
    48.              request.ContentLength = data.Length;  
    49.              outstream = request.GetRequestStream();  
    50.              outstream.Write(data, 0, data.Length);  
    51.              outstream.Close();  
    52.              //发送请求并获取相应回应数据  
    53.              response = request.GetResponse() as HttpWebResponse;  
    54.              //直到request.GetResponse()程序才开始向目标网页发送Post请求  
    55.              instream = response.GetResponseStream();  
    56.              sr = new StreamReader(instream, encoding);  
    57.              //返回结果网页(html)代码  
    58.              string content = sr.ReadToEnd();  
    59.              string err = string.Empty;  
    60.              return content;  
    61.          }  
    62.          catch (Exception ex)  
    63.          {  
    64.              string err = ex.Message;  
    65.              Response.Write(err);  
    66.              return string.Empty;  
    67.          }  
    68.      }  
  • 相关阅读:
    招聘.NET开发人员
    SQL 2005 SSIS 导入数据效率问题
    用户控件使用事件与调用页面交互
    使用sql语句删除标识列属性
    poj1520
    poj1476
    poj1363
    poj1477
    poj1312
    大端法小端法与union
  • 原文地址:https://www.cnblogs.com/Alex80/p/4259242.html
Copyright © 2011-2022 走看看