代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
微信拉取用户信息,主动推送消息
作者:
滄海一夢
/ 发布于
2013/8/20
/
1922
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 初始化创建 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 获取授权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 发起HTTP GET请求返回数据 * @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; } }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
滄海一夢
贡献的其它代码
(
1
)
.
微信拉取用户信息,主动推送消息
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3