事前思路准备

说在前面:如果要获取access_token,那么你需要appid,appsecret;另外需要post请求连接https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + 你的appid+ "&secret=" + 你的appsecret+ "";

(一)具体方法

/// <summary>
    /// 获得access_token
    /// </summary>
    /// <param name="appid"></param>
    /// <param name="appsecret"></param>
    /// <returns></returns>
    public string Get_Access_token(string appid, string appsecret)
    {
        WebClient webclient = new WebClient();
        string url = @"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret + "";
        byte[] bytes = webclient.DownloadData(url);//在指定的path上下载
        string result = Encoding.GetEncoding("utf-8").GetString(bytes);//转string
        JavaScriptSerializer js=new JavaScriptSerializer();
        access_token amodel= js.Deserialize<access_token>(result);//此处为定义的类,用以将json转成model
        string a_token=amodel.accesstoken;
        return a_token;
    }

(二)access_token 类

public class access_token {

public string accesstoken{get; set;}

public string expires_in{get;set;}

}

05-02 13:57
查看更多