一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - 微信java開發(fā)之實現(xiàn)微信主動推送消息

微信java開發(fā)之實現(xiàn)微信主動推送消息

2019-11-13 12:42java教程網(wǎng) JAVA教程

這篇文章主要介紹了微信開發(fā)過程中的使用java實現(xiàn)微信主動推送消息示例,需要的朋友可以參考下

1.拉取access_token
2.拉取用戶信息
3.主動推送消息
4.接口貌似要申請權(quán)限
5.依賴httpclient4.2.3 和jackson 2.2.1

 

復(fù)制代碼代碼如下:


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;
 }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 999久久久免费精品国产牛牛 | 欧美性色欧美a在线播放 | 成人高辣h视频一区二区在线观看 | 免费观看伦理片 | 99热精品成人免费观看 | 国产成人一区二区三区 | 好大夫在线个人空间 | 日韩欧美亚洲每日更新网 | 免费一区二区 | 黄瓜视频黄版 | 艾秋麻豆果冻传媒老狼仙踪林 | 国产小视频在线免费观看 | 久久精品国产亚洲AV热无遮挡 | 福利一区三区 | 亚洲毛片基地 | 亚洲一卡2卡4卡5卡6卡残暴在线 | 亚洲欧美成人综合久久久 | 69pao强力打造免费高速 | 亚洲 日本 中文字幕 制服 | 无罩看奶禁18 | 色臀网站| 亚洲444777KKK在线观看 | 护士videossexo另类 | 无码中文字幕av免费放 | 男人看片网址 | 超强台风免费观看完整版视频 | 19+韩国女主播激情vip视频在线 | 脱了白丝校花的内裤猛烈进入 | 日本不卡在线视频高清免费 | 亚洲天堂男人的天堂 | 91香蕉国产在线观看免费永久 | 91日本在线| 亚洲第一区在线观看 | 精品一久久香蕉国产线看观 | jiujiure精品| 久久久这里有精品999 | 天天狠天天天天透在线 | 免费大片a一级一级 | 日本春菜花在线中文字幕 | 女同性互吃奶乳免费视频 | 国内自拍第1页 |