微信java開(kāi)發(fā)之實(shí)現(xiàn)微信主動(dòng)推送消息
1.拉取access_token
2.拉取用戶信息
3.主動(dòng)推送消息
4.接口貌似要申請(qǐng)權(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}";
/**
* 主動(dòng)推送信息接口
*/
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請(qǐng)求返回?cái)?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工具類SendEmailUtil實(shí)現(xiàn)發(fā)送郵件
- java發(fā)送email一般步驟(實(shí)例講解)
- SSH框架網(wǎng)上商城項(xiàng)目第25戰(zhàn)之使用java email給用戶發(fā)送郵件
- Java HtmlEmail 郵件發(fā)送的簡(jiǎn)單實(shí)現(xiàn)代碼
- Java編程調(diào)用微信接口實(shí)現(xiàn)圖文信息推送功能
- 基于Tomcat7、Java、WebSocket的服務(wù)器推送聊天室實(shí)例
- java結(jié)合email實(shí)現(xiàn)自動(dòng)推送功能
相關(guān)文章
java解析xml匯總_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了java解析xml匯總_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理的相關(guān)資料,需要的朋友可以參考下2017-07-07
java根據(jù)當(dāng)前時(shí)間獲取yyyy-MM-dd?HH:mm:ss標(biāo)準(zhǔn)格式的時(shí)間代碼示例
在Java中可以使用java.time包中的LocalDateTime類和DateTimeFormatter類來(lái)獲取并格式化當(dāng)前時(shí)間為yyyy-MM-dd?HH:mm:ss的格式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
深入解析Java類加載的案例與實(shí)戰(zhàn)教程
本篇文章主要介紹Tomcat類加載器架構(gòu),以及基于類加載和字節(jié)碼相關(guān)知識(shí),去分析動(dòng)態(tài)代理的原理,對(duì)Java類加載相關(guān)知識(shí)感興趣的朋友一起看看吧2022-05-05
學(xué)習(xí)Java之Java中的異常處理機(jī)制詳解
在本文中,小編將帶領(lǐng)大家來(lái)學(xué)習(xí)Java的異常處理機(jī)制,包括異常機(jī)制、異常類型、如何捕獲異常、如何拋出異常以及如何創(chuàng)建自定義異常等核心內(nèi)容,感興趣的同學(xué)跟著小編一起來(lái)看看吧2023-08-08
Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA
本篇文章主要介紹了Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA,信息加密后數(shù)據(jù)更安全,需要的朋友可以參考下。2016-10-10
Spring Boot Cache使用方法整合代碼實(shí)例
這篇文章主要介紹了Spring Boot Cache使用方法整合代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

