package com.lianchuang.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lianchuang.entity.AccessToken;
import com.sun.jmx.snmp.Timestamp;
public class WeixinUtil {
public static AccessToken getAccessToken(String appid , String appsecret) throws Exception, IOException {
AccessToken accessToken = null;
JsonParser jsonparer = new JsonParser();// 初始化解析json格式的对象
String requestUrl = Constants.GET_ACCESSTOKEN_URL.replace("APPID" , appid).replace("APPSECRET" , appsecret);
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl);
HttpResponse res = client.execute(get);
String responseContent = null; // 响应内容
HttpEntity entity = res.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
JsonObject json = jsonparer.parse(responseContent).getAsJsonObject();
//JSONObject jsonObject = JSONObject.fromObject(json);
// 如果请求成功
if (null != json) {
try {
accessToken = new AccessToken();
accessToken.setAccessToken(json.get("access_token").getAsString());
accessToken.setExpiresIn(json.get("expires_in").getAsInt());
} catch (Exception e) {
accessToken = null;
// 获取token失败
System.out.println("获取token失败 errcode:{} errmsg:{}");
}
}
return accessToken;
}
public static AccessToken getAccessToken2(String appid , String appsecret) {
AccessToken accessToken = null;
String requestUrl = Constants.GET_ACCESSTOKEN_URL.replace("APPID" , appid).replace("APPSECRET" , appsecret);
String json = httpRequest(requestUrl , "GET" , null);
JSONObject jsonObject = JSONObject.fromObject(json);
// 如果请求成功
if (null != jsonObject) {
try {
accessToken = new AccessToken();
accessToken.setAccessToken(jsonObject.getString("access_token"));
accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
} catch (Exception e) {
accessToken = null;
// 获取token失败
System.out.println("获取token失败 errcode:{} errmsg:{}");
}
}
return accessToken;
}
public static String httpRequest(String requestUrl , String requestMethod , String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL" , "SunJSSE");
sslContext.init(null , tm , new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod)) httpUrlConn.connect();
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream , "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
// jsonObject = JSONObject.fromObject(buffer.toString());
return buffer.toString();
} catch (ConnectException ce) {
System.out.println("Weixin server connection timed out.");
} catch (Exception e) {
System.out.println("error.");
}
// return jsonObject;
return null;
}
public static void main(String[] args) {
WeixinUtil util = new WeixinUtil();
try {
//获取accessToken之前判断文件里有没有,没有重新获取,填写过期时间,有判断时间有没有过期
PropertiesUtil putil=new PropertiesUtil(Constants.CONF_PROPERTIES_PATH);
Long overduetime = Long.parseLong(putil.getProperty("OVERDUE_ACCESS_TOKEN_TIME"));
Timestamp s = new Timestamp();
AccessToken token = new AccessToken() ;
if(s.getDateTime()>overduetime){
//超时从新获取accessToken
token = util.getAccessToken2(Constants.APPID, Constants.SECRET);
putil.writeProperties("OVERDUE_ACCESS_TOKEN_TIME", String.valueOf(s.getDateTime()+Constants.EFFECTIVE_TIME));
putil.writeProperties("ACCESS_TOKEN", token.getAccessToken());
}else{
System.out.println("token:"+putil.getProperty("ACCESS_TOKEN")+",time:"+token.getExpiresIn());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于springMVC的java微信公众平台开发
共110个文件
jar:42个
class:22个
java:20个
5星 · 超过95%的资源 需积分: 50 2.6k 下载量 47 浏览量
2015-04-01
09:55:16
上传
评论 35
收藏 12.81MB ZIP 举报
温馨提示
java微信公众平台开发,基于springMVC技术和xstream,json等技术,通过使用图灵机器人接口,实现微信公众号的智能回复
资源推荐
资源详情
资源评论
收起资源包目录
基于springMVC的java微信公众平台开发 (110个子文件)
WeixinUtil.class 7KB
MessageUtil.class 4KB
TulingApiProcess.class 3KB
CoreService.class 3KB
PropertiesUtil.class 3KB
XxtController.class 3KB
SignUtil.class 3KB
testMenu.class 2KB
MessageResponse.class 2KB
BaseMessage.class 1KB
MessageUtil$1$1.class 1KB
Article.class 1KB
LoginController.class 1KB
FormatXmlProcess.class 1KB
NewsMessage.class 1KB
WeChat.class 1KB
MyX509TrustManager.class 893B
Constants.class 838B
AccessToken.class 787B
MessageUtil$1.class 734B
MenuClickService.class 639B
TextMessage.class 627B
.classpath 4KB
org.eclipse.wst.common.component 465B
org.eclipse.wst.jsdt.ui.superType.container 49B
aspectjweaver-1.5.2.jar 1.81MB
spring-core-3.2.4.RELEASE.jar 849KB
spring-context-3.2.4.RELEASE.jar 843KB
jackson-mapper-asl-1.9.3.jar 755KB
javassist-3.15.0-GA.jar 633KB
spring-webmvc-3.2.4.RELEASE.jar 622KB
spring-web-3.2.4.RELEASE.jar 611KB
spring-beans-3.2.4.RELEASE.jar 594KB
commons-collections-3.1.jar 546KB
standard.jar 497KB
xstream-1.4.3.jar 471KB
log4j-1.2.16.jar 470KB
spring-test-3.2.4.RELEASE.jar 447KB
antlr-2.7.7.jar 435KB
kaptcha-2.3.2.jar 421KB
spring-jdbc-3.2.4.RELEASE.jar 396KB
spring-orm-3.2.4.RELEASE.jar 378KB
httpclient-4.1.1.jar 343KB
spring-aop-3.2.4.RELEASE.jar 328KB
dom4j.jar 307KB
org.apache.felix.webconsole-1.0.0.jar 300KB
commons-lang-2.5.jar 273KB
spring-tx-3.2.4.RELEASE.jar 237KB
commons-beanutils-1.8.3.jar 227KB
jackson-core-asl-1.9.3.jar 223KB
spring-jms-3.2.4.RELEASE.jar 201KB
spring-expression-3.2.4.RELEASE.jar 192KB
gson-2.2.jar 185KB
httpcore-4.1.jar 177KB
commons-io-2.0.1.jar 156KB
json-lib-2.1-jdk15.jar 122KB
ezmorph-1.0.3.jar 76KB
spring-oxm-3.2.4.RELEASE.jar 72KB
commons-logging-1.1.1.jar 59KB
jboss-logging-3.1.0.GA.jar 59KB
spring-aspects-3.2.4.RELEASE.jar 50KB
aspectjrt-1.1.1.jar 29KB
slf4j-api-1.6.1.jar 25KB
jboss-transaction-api_1.1_spec-1.0.1.Final.jar 25KB
jstl.jar 21KB
xmlpull-1.1.3.1.jar 7KB
aopalliance-1.0.jar 4KB
WeixinUtil.java 6KB
MessageUtil.java 5KB
CoreService.java 4KB
TulingApiProcess.java 3KB
XxtController.java 3KB
SignUtil.java 3KB
PropertiesUtil.java 2KB
MessageResponse.java 2KB
testMenu.java 2KB
BaseMessage.java 1KB
Article.java 1KB
MyX509TrustManager.java 966B
WeChat.java 835B
FormatXmlProcess.java 834B
Constants.java 787B
NewsMessage.java 738B
MenuClickService.java 666B
LoginController.java 641B
AccessToken.java 609B
TextMessage.java 409B
.jsdtscope 500B
index.jsp 4KB
503.jsp 824B
test.jsp 820B
index.jsp 701B
403.jsp 606B
404.jsp 587B
500.jsp 578B
hello.jsp 432B
MANIFEST.MF 36B
.mymetadata 300B
org.eclipse.wst.jsdt.ui.superType.name 6B
com.genuitec.eclipse.j2eedt.core.prefs 1009B
共 110 条
- 1
- 2
贪杯的灵魂
- 粉丝: 32
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页