public static string getaccesstoken() throws clientprotocolexception,ioexception{string url = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential;url = url + appid= + 你的公众平台 开发 appid + secret= + 你的公众平台appsecr
public static string getaccesstoken() throws clientprotocolexception, ioexception { string url = https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential; url = url + &appid= + 你的公众平台开发appid + &secret= + 你的公众平台appsecret; defaulthttpclient client = new defaulthttpclient(); httpget httpget = new httpget(url); httpresponse httpresponse; httpresponse = client.execute(httpget); int code = httpresponse.getstatusline().getstatuscode(); string strresult = entityutils.tostring(httpresponse.getentity(),wxdata.charset); if (code == 200) { jsonobject jsonobject = jsonobject.fromobject(strresult); string token = jsonobject.getstring(access_token); system.out.println(token); } return null; }
每天接口访问次数为2000次,所以要保存到全局变量中,7200s的技能持续时间;
所需要的jar包
commons-beanutils
commons-collections
commons-httpclient
commons-lang
commons-logging
json-lib-2.3-jdk15
httpcore-4.2.2
httpmime-4.2.3
httpclient-cache-4.2.3
httpclient-4.2.3