欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信java開(kāi)發(fā)之實(shí)現(xiàn)微信主動(dòng)推送消息

 更新時(shí)間:2014年03月06日 09:38:15   作者:  
這篇文章主要介紹了微信開(kāi)發(fā)過(guò)程中的使用java實(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

復(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}";
 /**
  * 主動(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;
 }
}

相關(guān)文章

  • Java自然排序Comparable使用方法解析

    Java自然排序Comparable使用方法解析

    這篇文章主要介紹了Java自然排序Comparable使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 詳解MyBatis自定義Plugin插件

    詳解MyBatis自定義Plugin插件

    這篇文章主要介紹了MyBatis自定義Plugin插件的相關(guān)知識(shí),實(shí)現(xiàn)方法也很簡(jiǎn)單,只需實(shí)現(xiàn) Interceptor 接口,并指定想要攔截的方法簽名即可,需要的朋友可以參考下
    2018-06-06
  • Java適配器模式定義與用法示例

    Java適配器模式定義與用法示例

    這篇文章主要介紹了Java適配器模式定義與用法,結(jié)合具體實(shí)例形式分析了java適配器模式的功能、組成、定義、使用方法及適配程度等,需要的朋友可以參考下
    2017-06-06
  • Windows中Tomcat整合到Eclipse的圖文教程

    Windows中Tomcat整合到Eclipse的圖文教程

    下面小編就為大家?guī)?lái)一篇Windows中Tomcat整合到Eclipse的圖文教程。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • Spring中@PropertySource和@Value注解詳解

    Spring中@PropertySource和@Value注解詳解

    這篇文章主要介紹了Spring中@PropertySource和@Value注解詳解,@PropertySource注解可以方便和靈活的向Spring的環(huán)境容器(org.springframework.core.env.Environment Environment)中注入一些屬性,這些屬性可以在Bean中使用,需要的朋友可以參考下
    2023-11-11
  • 一文搞懂Spring AOP的五大通知類型

    一文搞懂Spring AOP的五大通知類型

    本文將詳細(xì)為大家介紹Spring AOP的五種通知類型(前置通知、后置通知、返回通知、異常通知、環(huán)繞通知),感興趣的朋友可以了解一下
    2022-06-06
  • SpringBoot JMX的基本使用方式

    SpringBoot JMX的基本使用方式

    這篇文章主要介紹了SpringBoot JMX的基本使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java?ReentrantLock并發(fā)鎖使用詳解

    java?ReentrantLock并發(fā)鎖使用詳解

    這篇文章主要為大家介紹了java?ReentrantLock并發(fā)鎖使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 使用@RequestBody配合@Valid校驗(yàn)入?yún)?shù)

    使用@RequestBody配合@Valid校驗(yàn)入?yún)?shù)

    這篇文章主要介紹了使用@RequestBody配合@Valid校驗(yàn)入?yún)?shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Springcloud eureka搭建高可用集群過(guò)程圖解

    Springcloud eureka搭建高可用集群過(guò)程圖解

    這篇文章主要介紹了Springcloud eureka搭建高可用集群過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04

最新評(píng)論