這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)如何在微信公眾號(hào)中獲取access_token,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元武昌做網(wǎng)站,已為上家服務(wù),為武昌各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
1.項(xiàng)目添加httpclient相關(guān)依賴,示例使用httpclient請求微信服務(wù)器,獲取微信返回結(jié)果。
org.apache.httpcomponents httpclient 4.5.3 org.apache.httpcomponents httpcore 4.4.6
2.httpClientUtil類,網(wǎng)上隨手找的 試了一下本例的doget方法 沒有問題,其他的 暫不考慮
public class HttpClientUtil { public static String doGet(String url, Mapparam) { // 創(chuàng)建Httpclient對象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { // 創(chuàng)建uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // 創(chuàng)建http GET請求 HttpGet httpGet = new HttpGet(uri); // 執(zhí)行請求 response = httpclient.execute(httpGet); // 判斷返回狀態(tài)是否為200 if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } public static String doGet(String url) { return doGet(url, null); } public static String doPost(String url, Map param) { // 創(chuàng)建Httpclient對象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 創(chuàng)建Http Post請求 HttpPost httpPost = new HttpPost(url); // 創(chuàng)建參數(shù)列表 if (param != null) { List paramList = new ArrayList<>(); for (String key : param.keySet()) { paramList.add(new BasicNameValuePair(key, param.get(key))); } // 模擬表單 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8"); httpPost.setEntity(entity); } // 執(zhí)行http請求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return resultString; } public static String doPost(String url) { return doPost(url, null); } public static String doPostJson(String url, String json) { // 創(chuàng)建Httpclient對象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 創(chuàng)建Http Post請求 HttpPost httpPost = new HttpPost(url); // 創(chuàng)建請求內(nèi)容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); // 執(zhí)行http請求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return resultString; } }
3.第三步就是簡單的測試代碼了
public class WeChatAccessTokenTest { public static void main(String[] args) { Mapparams = new HashMap<>(); // TODO: 2018/11/16 *號(hào)改成真實(shí)appid params.put("appid", "******"); // TODO: 2018/11/16 *號(hào)改成真實(shí)secret params.put("secret", "******"); params.put("grant_type", "client_credential"); String response = HttpClientUtil.doGet("https://api.weixin.qq.com/cgi-bin/token", params); JSONObject accessTokenObject = JSONObject.parseObject(response); String accessToken = accessTokenObject.getString("access_token"); Long expire = accessTokenObject.getLong("expires_in"); System.out.println(accessToken); } }
上述就是小編為大家分享的如何在微信公眾號(hào)中獲取access_token了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。