1.获取 获取用户发送的信息 、获取微信服务器IP地址 获取access_token
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;
}
}
2.封装返回用户的信息
package com.ll.wechart.util;
import java.util.List;
import com.ll.wechart.bean.WechartBackMessageBean;
import com.ll.wechart.bean.WechartBackNewsBean;
import com.ll.wechart.bean.WechartMessageBean;
public class BackWeChartFunction {
String responseStr = "Hello!";//默认返回消息
/**
* 返回文本信息
* @param mWechartMessageBean
* @param mWechartBackMessageBean
* @return
*/
public String backText(WechartMessageBean mWechartMessageBean,WechartBackMessageBean mWechartBackMessageBean){
StringBuffer revert = new StringBuffer();
revert.append("<xml>");
revert.append("<ToUserName><![CDATA["+mWechartMessageBean.getFromUserName()+"]]></ToUserName>");
revert.append("<FromUserName><![CDATA["+mWechartMessageBean.getToUserName()+"]]></FromUserName>");
revert.append("<CreateTime>"+GetWeChartFunction.getTimeStamp()+"]</CreateTime>");
revert.append("<MsgType><![CDATA[text]]></MsgType>");
revert.append("<Content><![CDATA["+mWechartBackMessageBean.getContent()+"]]></Content>");
revert.append("</xml>");
responseStr= revert.toString();
return responseStr;
}
/**
* 图片消息
* @param mWechartMessageBean
* @param mWechartBackMessageBean
* @return
*/
public String backImage(WechartMessageBean mWechartMessageBean,WechartBackMessageBean mWechartBackMessageBean){
StringBuffer revert = new StringBuffer();
revert.append("<xml>");
revert.append("<ToUserName><![CDATA["+mWechartMessageBean.getFromUserName()+"]]></ToUserName>");
revert.append("<FromUserName><![CDATA["+mWechartMessageBean.getToUserName()+"]]></FromUserName>");
revert.append("<CreateTime>"+GetWeChartFunction.getTimeStamp()+"]</CreateTime>");
revert.append("<MsgType><![CDATA[image]]></MsgType>");
revert.append("<Image>");
revert.append("<MediaId><![CDATA["+mWechartBackMessageBean.getMediaId()+"]]></MediaId>");
revert.append("</Image>");
revert.append("</xml>");
responseStr= revert.toString();
return responseStr;
}
/**
* 语音消息
* @param mWechartMessageBean
* @param mWechartBackMessageBean
* @return
*/
public String backVoice(WechartMessageBean mWechartMessageBean,WechartBackMessageBean mWechartBackMessageBean){
StringBuffer revert = new StringBuffer();
revert.append("<xml>");
revert.append("<ToUserName><![CDATA["+mWechartMessageBean.getFromUserName()+"]]></ToUserName>");
revert.append("<FromUserName><![CDATA["+mWechartMessageBean.getToUserName()+"]]></FromUserName>");
revert.append("<CreateTime>"+GetWeChartFunction.getTimeStamp()+"]</CreateTime>");
revert.append("<MsgType><![CDATA[voice]]></MsgType>");
revert.append("<Voice>");
revert.append("<MediaId><![CDATA["+mWechartBackMessageBean.getMediaId()+"]]></MediaId>");
revert.append("</Voice>");
revert.append("</xml>");
responseStr= revert.toString();
return responseStr;
}
/**
* 视屏消息
* @param mWechartMessageBean
* @param mWechartBackMessageBean
* @return
*/
public String backVideo(WechartMessageBean mWechartMessageBean,WechartBackMessageBean mWechartBackMessageBean){
StringBuffer revert = new StringBuffer();
revert.append("<xml>");
revert.append("<ToUserName><![CDATA["+mWechartMessageBean.getFromUserName()+"]]></ToUserName>");
revert.append("<FromUserName><![CDATA["+mWechartMessageBean.getToUserName()+"]]></FromUserName>");
revert.append("<CreateTime>"+GetWeChartFunction.getTimeStamp()+"]</CreateTime>");
revert.append("<MsgType><![CDATA[video]]></MsgType>");
revert.append("<Video>");
revert.append("<MediaId><![CDATA["+mWechartBackMessageBean.getMediaId()+"]]></MediaId>");
revert.append("<Title><![CDATA["+mWechartBackMessageBean.getTitle()+"]]></Title>");
revert.append("<Description><![CDATA["+mWechartBackMessageBean.getDescription()+"]]></Description>");
revert.append("</Video>");
revert.append("</xml>");
responseStr= revert.toString();
return responseStr;
}
/**
* 音乐消息
* @param mWechartMessageBean
* @param mWechartBackMessageBean
* @return
*/
public String backMusic(WechartMessageBean mWechartMessageBean,WechartBackMessageBean mWechartBackMessageBean){
StringBuffer revert = new StringBuffer();
revert.append("<xml>");
revert.append("<ToUserName><![CDATA["+mWechartMessageBean.getFromUserName()+"]]></ToUserName>");
revert.append("<FromUserName><![CDATA["+mWechartMessageBean.getToUserName()+"]]></FromUserName>");
revert.append("<CreateTime>"+GetWeChartFunction.getTimeStamp()+"]</CreateTime>");
revert.append("<MsgType><![CDATA[music]]></MsgType>");
revert.append("<Music>");
revert.append("<Title><![CDATA["+mWechartBackMessageBean.getTitle()+"]]></Title>");
revert.append("<Description><![CDATA["+mWechartBackMessageBean.getDescription()+"]]></Description>");
revert.append("<MusicUrl><![CDATA["+mWechartBackMessageBean.getMusicUrl()+"]]></MusicUrl>");
revert.append("<HQMusicUrl><![CDATA["+mWechartBackMessageBean.getHQMusicUrl()+"]]></HQMusicUrl>");
revert.append("<ThumbMediaId><![CDATA["+mWechartBackMessageBean.getThumbMediaId()+"]]></ThumbMediaId>");
revert.append("</Music>");
revert.append("</xml>");
responseStr= revert.toString();
return responseStr;
}
/**
* 返回图文消息
* @param mWechartMessageBean
* @param mWechartBackMessageBean
* @param mWechartBackNewsBean
* @return
*/
public String backNews(WechartMessageBean mWechartMessageBean,WechartBackMessageBean mWechartBackMessageBean,List<WechartBackNewsBean> mWechartBackNewsBeanList){
StringBuffer revert = new StringBuffer();
revert.append("<xml>");
revert.append("<ToUserName><![CDATA["+mWechartMessageBean.getFromUserName()+"]]></ToUserName>");
revert.append("<FromUserName><![CDATA["+mWechartMessageBean.getToUserName()+"]]></FromUserName>");
revert.append("<CreateTime>"+GetWeChartFunction.getTimeStamp()+"]</CreateTime>");
revert.append("<MsgType><![CDATA[news]]></MsgType>");
int size =mWechartBackNewsBeanList.size();
revert.append("<ArticleCount><"+size+"></ArticleCount>");
revert.append("<Articles>");
for (int i = 0; i < size ; i++) {
WechartBackNewsBean mWechartBackNewsBean = mWechartBackNewsBeanList.get(i);
revert.append("<item>");
revert.append("<Title><![CDATA["+mWechartBackNewsBean.getTitle()+"]]></Title>");
revert.append("<Description><![CDATA["+mWechartBackNewsBean.getDescription()+"]]></Description>");
revert.append("<PicUrl><![CDATA["+mWechartBackNewsBean.getPicUrl()+"]]></PicUrl>");
revert.append("<Url><![CDATA["+mWechartBackNewsBean.getUrl()+"]]></Url>");
revert.append("</item>");
}
revert.append("</Articles>");
revert.append("</xml>");
responseStr= revert.toString();
return responseStr;
}
}
3.辅助javabean
package com.ll.wechart.bean;
/**
* 文本消息(text) 图片消息(image) 语音消息(voice) 视频消息(video) 小视频消息(shortvideo)
* 地理位置消息(location) 链接消息(link)
*
* @author liuli
*
*/
public class WechartBackMessageBean {
private String Content = "";// 文本消息内容
private String MediaId = "";// 图片消息媒体id,可以调用多媒体文件下载接口拉取数据。||语音消息媒体id,可以调用多媒体文件下载接口拉取数据。||视频消息媒体id,可以调用多媒体文件下载接口拉取数据。
private String MusicUrl = "";// 音乐链接
private String HQMusicUrl = "";// 高质量音乐链接,WIFI环境优先使用该链接播放音乐
private String ThumbMediaId = "";// 视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
private String Title = "";// 消息标题
private String Description = "";// 消息描述
public String getMusicUrl() {
return MusicUrl;
}
public void setMusicUrl(String musicUrl) {
MusicUrl = musicUrl;
}
public String getHQMusicUrl() {
return HQMusicUrl;
}
public void setHQMusicUrl(String hQMusicUrl) {
HQMusicUrl = hQMusicUrl;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
public String getThumbMediaId() {
return ThumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
ThumbMediaId = thumbMediaId;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
=====
package com.ll.wechart.bean;
/**
* 图文消息
* @author liuli
*
*/
public class WechartBackNewsBean {
private String Title="";// 消息标题
private String Description="";// 消息描述
private String Url="";// 消息链接
private String PicUrl ="";//图片链接
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
}
====
package com.ll.wechart.bean;
/**
* 文本消息(text)
* 图片消息(image)
* 语音消息(voice)
* 视频消息(video)
* 小视频消息(shortvideo)
* 地理位置消息(location)
* 链接消息(link)
* @author liuli
*
*/
public class WechartMessageBean {
private String ToUserName="";// 开发者微信号
private String FromUserName="";// 发送方帐号(一个OpenID)
private String CreateTime="";// 消息创建时间 (整型)
private String MsgType="";// text
private String Content="";// 文本消息内容
private String PicUrl ="";//图片链接
private String MediaId ="";//图片消息媒体id,可以调用多媒体文件下载接口拉取数据。||语音消息媒体id,可以调用多媒体文件下载接口拉取数据。||视频消息媒体id,可以调用多媒体文件下载接口拉取数据。
private String Format ="";//语音格式,如amr,speex等
private String ThumbMediaId ="";//视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
private String Location_X="";// 地理位置维度
private String Location_Y="";// 地理位置经度
private String Scale="";// 地图缩放大小
private String Label="";// 地理位置信息
private String Title="";// 消息标题
private String Description="";// 消息描述
private String Url="";// 消息链接
private String MsgId="";// 消息id,64位整型
private String Encrypt ="";//加密的报文 事件类型,
public String getEvent() {
return Event;
}
public void setEvent(String event) {
Event = event;
}
public String getEventKey() {
return EventKey;
}
public void setEventKey(String eventKey) {
EventKey = eventKey;
}
//事件
String Event ="";
String EventKey ="";
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
public void setFromUserName(String fromUserName) {
FromUserName = fromUserName;
}
public String getCreateTime() {
return CreateTime;
}
public void setCreateTime(String createTime) {
CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String msgType) {
MsgType = msgType;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
public String getFormat() {
return Format;
}
public void setFormat(String format) {
Format = format;
}
public String getThumbMediaId() {
return ThumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
ThumbMediaId = thumbMediaId;
}
public String getLocation_X() {
return Location_X;
}
public void setLocation_X(String location_X) {
Location_X = location_X;
}
public String getLocation_Y() {
return Location_Y;
}
public void setLocation_Y(String location_Y) {
Location_Y = location_Y;
}
public String getScale() {
return Scale;
}
public void setScale(String scale) {
Scale = scale;
}
public String getLabel() {
return Label;
}
public void setLabel(String label) {
Label = label;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public String getMsgId() {
return MsgId;
}
public void setMsgId(String msgId) {
MsgId = msgId;
}
public String getEncrypt() {
return Encrypt;
}
public void setEncrypt(String encrypt) {
Encrypt = encrypt;
}
}
代码结构