java开发之实现主动推送消息
1.拉取access_token
2.拉取⽤户信息
3.主动推送消息
4.接⼝貌似要申请权限
5.依赖httpclient4.2.3 和jackson 2.2.1
复制代码代码如下:
public class WeixinAPIHelper {
/**
* 获取token接⼝
*/
private String getTokenUrl = "api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; /**
* 拉⽤户信息接⼝
*/
private String getUserInfoUrl = "api.weixin.qq/cgi-bin/user/info?access_token={0}&openid={1}";
/**
* 主动推送信息接⼝
*/
private String sendMsgUrl = "api.weixin.qq/cgi-bin/message/send?access_token={0}";
private HttpClient webClient;
private Log log = Log(getClass());
public void initWebClient(String proxyHost, int proxyPort){
this.initWebClient();
if(webClient != null && !StringUtils.isEmpty(proxyHost)){
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
}
}
/**
* @desc 初始化创建 WebClient
*/
public void initWebClient() {
log.info("");
try {
PoolingClientConnectionManager tcm = new PoolingClientConnectionManager();
tcm.setMaxTotal(10);
SSLContext ctx = Instance("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);
webClient = new DefaultHttpClient(tcm);
} catch (Exception ex) {
<("initWebClient exception", ex);
} finally {
log.info("");
}
}
/**
* @desc 获取授权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.TokenUrl, appid, secret);
String response = executeHttpGet(url);
accessToken = ad(response, "access_token");
} catch (Exception e) {
<("get access toekn exception", e);
}
return accessToken;
}
/**
* @desc 推送信息
* @param token
* @param msg
* @return
*/
public String sendMessage(String token,String msg){
try{
log.info("ken:"+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) ute(post, responseHandler);  log.info("return response=====start======");
log.info(response);
log.info("return response=====end======");
return response;
}catch (Exception e) {
<("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.UserInfoUrl, token, openid);
String response = executeHttpGet(url);
代码转换JsonNode json = ad(response);
if (("openid") != null) {
WeixinOpenUser user = new WeixinOpenUser();
user.("openid").asText());
user.("subscribe").asText());
if ("1".State())) {
user.("nickname").asText());
user.("sex").asText());
user.("city").asText());
user.("language").asText());
}
return user;
}
} catch (Exception e) {
<("get user info exception", e);
}
return null;
}
/**
* @desc 发起HTTP GET请求返回数据
* @param url
* @return
* @throws IOException
* @throws ClientProtocolException
*/
private String executeHttpGet(String url) throws IOException, ClientProtocolException {  ResponseHandler<?> responseHandler = new BasicResponseHandler();
String response = (String) ute(new HttpGet(url), responseHandler);  log.info("return response=====start======");
log.info(response);
log.info("return response=====end======");
return response;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。