這篇文章主要介紹了利用java開發(fā)微信如何實現(xiàn)微信主動推送消息,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、做網(wǎng)站、濟源網(wǎng)絡(luò)推廣、小程序定制開發(fā)、濟源網(wǎng)絡(luò)營銷、濟源企業(yè)策劃、濟源品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供濟源建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
利用java開發(fā)微信實現(xiàn)微信主動推送消息實例
1.拉取access_token
2.拉取用戶信息
3.主動推送消息
4.接口貌似要申請權(quán)限
5.依賴httpclient4.2.3 和jackson 2.2.1
代碼如下:
public class WeixinAPIHelper { /** * 獲取token接口 */ private String getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; /** * 拉微信用戶信息接口 */ private String getUserInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}"; /** * 主動推送信息接口 */ private String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/send?access_token={0}"; private HttpClient webClient; private Log log = LogFactory.getLog(getClass()); public void initWebClient(String proxyHost, int proxyPort){ this.initWebClient(); if(webClient != null && !StringUtils.isEmpty(proxyHost)){ HttpHost proxy = new HttpHost(proxyHost, proxyPort); webClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); } } /** * @desc 初始化創(chuàng)建 WebClient */ public void initWebClient() { log.info("initWebClient start...."); try { PoolingClientConnectionManager tcm = new PoolingClientConnectionManager(); tcm.setMaxTotal(10); SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new X509TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme sch = new Scheme("https", 443, ssf); tcm.getSchemeRegistry().register(sch); webClient = new DefaultHttpClient(tcm); } catch (Exception ex) { log.error("initWebClient exception", ex); } finally { log.info("initWebClient end...."); } } /** * @desc 獲取授權(quán)token * @param appid * @param secret * @return */ public String getAccessToken(String appid, String secret) { String accessToken = null; try { log.info("getAccessToken start.{appid=" + appid + ",secret:" + secret + "}"); String url = MessageFormat.format(this.getTokenUrl, appid, secret); String response = executeHttpGet(url); accessToken = JsonUtils.read(response, "access_token"); } catch (Exception e) { log.error("get access toekn exception", e); } return accessToken; } /** * @desc 推送信息 * @param token * @param msg * @return */ public String sendMessage(String token,String msg){ try{ log.info("sendMessage start.token:"+token+",msg:"+msg); String url = MessageFormat.format(this.sendMsgUrl, token); HttpPost post = new HttpPost(url); ResponseHandler> responseHandler = new BasicResponseHandler(); StringEntity entity = new StringEntity(msg); post.setEntity(entity); String response = (String) this.webClient.execute(post, responseHandler); log.info("return response=====start======"); log.info(response); log.info("return response=====end======"); return response; }catch (Exception e) { log.error("get user info exception", e); return null; } } /** * @desc 拉取用戶信息 * @param token * @param openid * @return */ public WeixinOpenUser getUserInfo(String token, String openid) { try { log.info("getUserInfo start.{token:" + token + ",openid:" + openid + "}"); String url = MessageFormat.format(this.getUserInfoUrl, token, openid); String response = executeHttpGet(url); JsonNode json = JsonUtils.read(response); if (json.get("openid") != null) { WeixinOpenUser user = new WeixinOpenUser(); user.setOpenUserId(json.get("openid").asText()); user.setState(json.get("subscribe").asText()); if ("1".equals(user.getState())) { user.setUserName(json.get("nickname").asText()); user.setSex(json.get("sex").asText()); user.setCity(json.get("city").asText()); user.setLanguage(json.get("language").asText()); } return user; } } catch (Exception e) { log.error("get user info exception", e); } return null; } /** * @desc 發(fā)起HTTP GET請求返回數(shù)據(jù) * @param url * @return * @throws IOException * @throws ClientProtocolException */ private String executeHttpGet(String url) throws IOException, ClientProtocolException { ResponseHandler> responseHandler = new BasicResponseHandler(); String response = (String) this.webClient.execute(new HttpGet(url), responseHandler); log.info("return response=====start======"); log.info(response); log.info("return response=====end======"); return response; } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“利用java開發(fā)微信如何實現(xiàn)微信主動推送消息”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!