公共类weixinapihelper {
//*
privatestringgettokenurl=' https://API.weixin.QQ.com/CGI-bin/token? grant _ type=client _ credential appid={0} secret={1} ';
//*
privatestringgetuserinfourl=' https://API.weixin.QQ.com/CGI-bin/user/info? access_token={0}openid={1} ';
//*
privatestringsendmsgurl=' https://API.weixin.QQ.com/CGI-bin/message/send? access_token={0} ';
private http客户端webclient;
private loglog=log factory.get log (getclass ();
publicvoidinitwebclient (stringproxyhost,int proxyPort ) )。
this.initWebClient (;
互联网客户端!=空! stringutils.isempty(proxyhost ) }
webClient.getParams ().set parameter (connroutepnames.default _ proxy,proxy );
}
}
//*
公共void init webclient (
log.info(initWebClientstart…);
try {
poolingclientconnectionmanagertcm=newpoolingclientconnectionmanager (;
TCM.setmaxtotal(10;
x509 trustmanagertm=new x509 trust manager (
publicvoidcheckclienttrusted (x509 certificate [ ] arg 0,String arg1) throws CertificateException {
}
publicvoidcheckservertrusted (x509 certificate [ ] arg 0,String arg1) throws CertificateException {
}
公共x509认证[ ] getacceptedissuers () )。
返回空值;
}
(;
CTX.init(null,new X509TrustManager[] { tm },null );
slsocketfactoryssf=newsslsocketfactory (CTX,SSL socket factory.allow _ all _ hostname _ verifier );
schemesch=newscheme('https ',443,ssf );
tcm.getSchemeRegistry ().register (sch );
webclient=newdefaulthttpclient (TCM;
}catch(exceptionex ) {
log.error (initwebclientexception ),ex );
} finally {
log.info(initwebclientend…);
}
}
//*
publicstringgetaccesstoken (string appid,String secret ) {
try {
log.info (getaccesstokenstart.{ appid=' appid ',secret:' secret '} );
}catch(exceptione ) {
log.error (' getaccesstoeknexception ',e );
}
return accessToken;
}
//*
公共字符串传感(string token,String msg )。
try{
log.info (sendmessage start.token : ) token )、msg: ) msg );
responsehandlerresponsehandler=newbasicresponsehandler (;
post.setentity(entity;
log.info (return response=====start=====' );
log.info(response;
log.info (' return response====end====' );
返回响应;
}catch(exceptione ) {
log.error (' getuserinfoexception ',e );
返回空值;
}
}
//*
publicweixinopenusergetuserinfo (string token,String openid ) {
try {
log.info (getuser infostart.{ token : ' token ',openid:' openid '} );
jonnodejson=JSON utils.read (response;
if(JSON.get(openid )!=null ) {
user.setopenuserid (JSON.get (OpenID ) ).asText );
user.setstate (JSON.get (subscribe ) ).asText );
if('1'.equals(user.getstate ) ) )0)
user.set username (JSON.get (' nickname ' ).asText ) );
user.setsex(JSON.get('sex ' ).asText );
user.setcity(JSON.get ) (city ).asText );
user.set language (JSON.get (language ) ).asText );
}
返回用户;
}
}catch(exceptione ) {
log.error (' getuserinfoexception ',e );
}
返回空值;
}
//*
privatestringexecutehttpget (string URL ) throws IOException,ClientProtocolException {
responsehandlerresponsehandler=newbasicresponsehandler (;
log.info (return response=====start=====' );
log.info(response;
log.info (' return response====end====' );
返回响应;
}
}