微信开发工具类,包括获取获取access_token、获取用户发送的信息、获取微信服务器IP地址。
package com.ll.wechart.util;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import com.ll.wechart.bean.WechartMessageBean;
import com.ll.xml.XmlUtil;
public class GetWeChartFunction {
private Log log = LogFactory.getLog(this.getClass().getName());
String resp ="";//调用返回信息
public static void main(String[] args) {
GetWeChartFunction wc= new GetWeChartFunction();
//System.out.println(wc.getAccess_token());
//System.out.println(wc.getServerIP(WeChartPams.AccessToken));
System.out.println(System.currentTimeMillis());
}
/**
* 系统时间戳
*/
public static String getTimeStamp(){
String timestamp=String.valueOf(System.currentTimeMillis());
return timestamp;
}
/**
* 获取用户发送的信息
* @param xmlInfo
* @return
*/
public WechartMessageBean getMessgeUtil(String xmlInfo){
WechartMessageBean mWechartMessageBean = new WechartMessageBean();
try {
Document document = null;
document = XmlUtil.stringToXML(xmlInfo);
Element rootElement = document.getRootElement(); // 取得根元素
mWechartMessageBean = (WechartMessageBean) XmlUtil.xmlToBean(mWechartMessageBean, rootElement);
} catch (Exception e) {
log.error(e);
return null;
}
return mWechartMessageBean;
}
/**
* 获取微信服务器IP地址
* 如果公众号基于安全等考虑,需要获知微信服务器的IP地址列表,以便进行相关限制,可以通过该接口获得微信服务器IP地址列表。
* @param accessToken
* @return
*/
public String getServerIP(String accessToken){
String urlstr = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token="+accessToken+"";
try{
HttpURLConnection http = (HttpURLConnection) new URL(urlstr).openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000");//读取超时30秒
InputStream is = http.getInputStream();
int size = is.available();
byte[] buf = new byte[size];
is.read(buf);
resp = new String(buf,"UTF-8");
} catch (Exception e) {
log.error(e);
}
return resp;
}
/**
* @author liuli
* 获取access_token
* access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。
* 开发者需要进行妥善保存。
* access_token的存储至少要保留512个字符空间。
* access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。
* 4jDowhQhY8Zr0CMoOhqc0uyvLX8oLOG2c-okF-mS8IGMBpO4gTBRX9d8-lC0aST3MRqjisj_P9ueYgvI1GdtxALIB01MW6GiPozibBonHe4
*/
public String getAccess_token(){
String urlstr = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+WeChartPams.AppID+"&secret="+WeChartPams.AppSecret+"";
try{
HttpURLConnection http = (HttpURLConnection) new URL(urlstr).openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000");//读取超时30秒
InputStream is = http.getInputStream();
int size = is.available();
byte[] buf = new byte[size];
is.read(buf);
String resp = new String(buf,"UTF-8");
log.info("back info:" + resp);
} catch (Exception e) {
log.error(e);
}
return resp;
}
}
转自:http://wenwang.iteye.com/blog/2228668