您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringBoot小程序推送信息怎么實現(xiàn)的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇SpringBoot小程序推送信息怎么實現(xiàn)文章都會有所收獲,下面我們一起來看看吧。
首先申請一個小程序,微信開放平臺:小程序
appid
AppSecret
subscribeMessage.send | 微信開放文檔
引入依賴
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
先準(zhǔn)備一個HTTP工具類Z
import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; import org.apache.http.util.EntityUtils; import java.io.IOException; /** * @author lrx * @description: TODO * @date 2021/3/9 9:50 */ public class HttpClientUtils { //Http協(xié)議GET請求 public static String httpGet(String url) throws IOException { //初始化HttpClient CloseableHttpClient httpClient = HttpClients.createDefault(); //創(chuàng)建HttpGet HttpGet httpGet = new HttpGet(url); //發(fā)起請求,獲取response對象 CloseableHttpResponse response = httpClient.execute(httpGet); //獲取請求狀態(tài)碼 //response.getStatusLine().getStatusCode(); //獲取返回數(shù)據(jù)實體對象 HttpEntity entity = response.getEntity(); //轉(zhuǎn)為字符串 String result = EntityUtils.toString(entity, "UTF-8"); return result; } //Http協(xié)議Post請求 public static String httpPost(String url, String json) throws Exception { //初始HttpClient CloseableHttpClient httpClient = HttpClients.createDefault(); //創(chuàng)建Post對象 HttpPost httpPost = new HttpPost(url); //設(shè)置Content-Type /* httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");*/ StringEntity se = new StringEntity(json,"UTF-8"); se.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(se); //發(fā)起請求,獲取response對象 CloseableHttpResponse response = httpClient.execute(httpPost); //獲取請求碼 //response.getStatusLine().getStatusCode(); //獲取返回數(shù)據(jù)實體對象 HttpEntity entity = response.getEntity(); //轉(zhuǎn)為字符串 String result = EntityUtils.toString(entity, "UTF-8"); return result; } //Https協(xié)議Get請求 public static String httpsGet(String url) throws Exception { CloseableHttpClient hp = createSSLClientDefault(); HttpGet hg = new HttpGet(url); CloseableHttpResponse response = hp.execute(hg); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity, "UTF-8"); hp.close(); return content; } //Https協(xié)議Post請求 public static String httpsPost(String url, String json) throws Exception { CloseableHttpClient hp = createSSLClientDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(json)); CloseableHttpResponse response = hp.execute(httpPost); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity, "UTF-8"); hp.close(); return content; } public static CloseableHttpClient createSSLClientDefault() throws Exception { //如果下面的方法證書還是不過,報錯的話試試下面第二種 /* SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){ //信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build();*/ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } }
測試代碼
import com.alibaba.fastjson.JSONObject; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author lrx * @description: TODO 小程序推送 * @date 2022/4/11 13:32 */ public class TestXCXMain { public static void main(String[] args) throws Exception { String appid = ""; //appid String secret = ""; //secret //登錄鏈接獲取token String loginUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; Map<String, Object> payloadMap = JSONObject.parseObject(HttpClientUtils.httpGet(loginUrl)); String token = null; if (payloadMap.containsKey("access_token")) { token = payloadMap.get("access_token").toString(); } System.out.println("獲取token" + token); Map<String, Object> qqMap = new HashMap<String, Object>(); qqMap.put("touser", "openid"); //要推送的openid qqMap.put("template_id", ""); //信息模板id qqMap.put("page", "index"); qqMap.put("miniprogram_state", "developer"); qqMap.put("lang", "zh_CN"); //封裝信息 Map<String, Object> dataMap = new HashMap<String, Object>(); Map<String, String> valueMap1 = new HashMap<String, String>(); valueMap1.put("value", "成功"); Map<String, String> valueMap2 = new HashMap<String, String>(); valueMap2.put("value", "成功"); dataMap.put("thing3", valueMap1); dataMap.put("thing1", valueMap2); qqMap.put("data", dataMap); //發(fā)送 Map<String, Object> payloadMapData = JSONObject.parseObject(HttpClientUtils.httpPost("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+token,JSONObject.toJSONString(qqMap))); if (payloadMapData.containsKey("errCode")) { System.out.println("返回code碼:"+payloadMapData.get("errCode").toString()); } } }
關(guān)于“SpringBoot小程序推送信息怎么實現(xiàn)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“SpringBoot小程序推送信息怎么實現(xiàn)”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。