1. Pull Access_Token
2. Puxe as informações do usuário
3. Empurre ativamente as mensagens
4. A interface parece exigir permissão
5. confiando em httpclient4.2.3 e Jackson 2.2.1
A cópia do código é a seguinte:
classe pública weixinapihelper {
/**
* Obtenha a interface do token
*/
private String getTokenurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= (
/**
* Puxe a interface de informações do usuário do WeChat
*/
Private String getUserInfourl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token= =
/**
* Interface de informação push ativa
*/
private string sendmsgurl = "https://api.weixin.qq.com/cgi-bin/message/send?access_token= =}";
WebClient privado httpclient;
log privado log = logFactory.getLog (getClass ());
public void initwebclient (String proxyhost, int proxyport) {
this.initwebclient ();
if (webclient! = null &&! stringUtils.isEmpty (proxyhost)) {
Httphot proxy = new httpHost (proxyhost, proxyport);
webclient.getParams (). SetParameter (ConnRoutepNames.Default_Proxy, Proxy);
}
}
/**
* @Desc inicialize a criação de webclient
*/
public void initwebclient () {
log.info ("initwebclient start ....");
tentar {
PoolingClientConnectionManager tcm = new PoolingClientConnectionManager ();
tcm.setmaxtotal (10);
SslContext ctx = sslContext.getInstance ("tls");
X509TrustManager tm = novo x509TrustManager () {
public void checkclienttrusted (x509Certificate [] arg0, string arg1) lança certificado de CertateException {
}
public void checkServerTrusted (x509Certificate [] arg0, string arg1) lança certifiqueexception {
}
public x509Certificate [] getAcceptEdissuers () {
retornar nulo;
}
};
ctx.init (null, novo x509TrustManager [] {tm}, null);
SSLSocketFactory SSF = new SSLSocketFactory (ctx, sslsocketFactory.allow_all_hostname_verifier);
Esquema sch = novo esquema ("https", 443, ssf);
tcm.getschemeregistry (). registro (sch);
webclient = new DefaulthttpClient (TCM);
} catch (Exceção ex) {
log.error ("exceção initwebclient", ex);
} finalmente {
log.info ("initwebclient end ...");
}
}
/**
* @Desc Obtenha token autorizado
* @param appid
* @param segredo
* @retornar
*/
public String getAccessToken (String Appid, String secret) {
String accessToken = null;
tentar {
log.info ("GetAccessToken Start. {Appid =" + Appid + ", Secret:" + Secret + "}");
String url = messageFormat.format (this.gettokenurl, appid, secret);
String Response = ExecuteHttpGet (URL);
accessToken = jsonutils.read (resposta, "access_token");
} catch (Exceção e) {
log.error ("Obtenha acesso à exceção", e);
}
Retornar AccessToken;
}
/**
* @DESC Push Information
* @param token
* @param msg
* @retornar
*/
public string sendMessage (string token, string msg) {
tentar{
log.info ("sendMessage start.token:"+token+", msg:"+msg);
String url = messageFormat.format (this.sendmsgurl, token);
Httppost post = novo httppost (url);
ResponseHandler <?> ResponseHandler = new BasicResponseHandler ();
Stringentity entity = new stringentity (msg);
post.setentity (entidade);
String Response = (String) this.webclient.execute (post, ResponseHandler);
log.info ("Retornar Resposta ====== START ====================);
log.info (resposta);
log.info ("Resposta de retorno ===== end ======================);
resposta de retorno;
} catch (Exceção e) {
log.error ("Obtenha a exceção de informações do usuário", e);
retornar nulo;
}
}
/**
* @Desc Puxe Informações do usuário
* @param token
* @param openId
* @retornar
*/
public weixinopenUser getUserinfo (string token, string openId) {
tentar {
log.info ("getUserinfo start. {token:" + token + ", OpenId:" + OpenId + "}");
String url = messageFormat.format (this.getUserinfourl, token, OpenID);
String Response = ExecuteHttpGet (URL);
JsonNode json = jsonutils.read (resposta);
if (json.get ("OpenId")! = null) {
WeixinopenUser User = new WeixinOpenUser ();
user.setOpenUserID (json.get ("OpenId"). ASTEXT ());
user.setState (json.get ("assinando"). ASTEXT ());
if ("1" .equals (user.getState ())) {
user.setUserName (json.get ("apelido"). astext ());
user.setSex (json.get ("sexo"). ASTEXT ());
user.setCity (json.get ("city"). ASTEXT ());
user.setLanguage (json.get ("idioma"). ASTEXT ());
}
devolver usuário;
}
} catch (Exceção e) {
log.error ("Obtenha a exceção de informações do usuário", e);
}
retornar nulo;
}
/**
* @Desc inicia o http get solicitar para retornar dados
* @param url
* @retornar
* @THOWSows IoException
* @Throws ClientProtocolexception
*/
private string executehttpget (string url) lança ioexception, clientprotocolexception {
ResponseHandler <?> ResponseHandler = new BasicResponseHandler ();
String Response = (String) this.webclient.execute (novo httpget (URL), ResponseHandler);
log.info ("Retornar Resposta ====== START ====================);
log.info (resposta);
log.info ("Resposta de retorno ===== end ======================);
resposta de retorno;
}
}