import java.io.ioexception;
import java.io.printwriter;
import java.util.date;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import com.eiyoung.wechat.web.utils.message;
import com.eiyoung.wechat.web.utils.replymessage;
import org.apache.commons.io.ioutils;
import com.thoughtworks.xstream.xstream;
import com.thoughtworks.xstream.io.xml.domdriver;
public class wechat extends httpservlet {
private static final long serialversionuid = 1l;
public wechat() {
super();
}
/**
* 验证
* @param request
* @param response
* @throws servletexception
* @throws ioexception
*/
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
response.setcontenttype("text/html;charset=utf-8");
printwriter pw = response.getwriter();
string echo = request.getparameter("echostr");
system.out.print(echo);
echo = new string(echo.getbytes("iso-8859-1"),"utf-8");
pw.println(echo);
}
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
response.setcontenttype("text/html;charset=utf-8");
printwriter pw = response.getwriter();
string wxmsgxml = ioutils.tostring(request.getinputstream(),"utf-8");
message textmsg = null;
try {
textmsg = getmessage(wxmsgxml);
} catch (exception e) {
e.printstacktrace();
}
stringbuffer replymsg = new stringbuffer();
if(textmsg != null){
//增加你所需要的处理逻辑,这里只是简单重复消息
replymsg.append("您给我的消息是:");
replymsg.append(textmsg.getcontent());
}
else{
replymsg.append(":)不是文本的消息,我暂时看不懂");
}
string returnxml = getreplymessage(replymsg.tostring(), textmsg.getfromusername(),textmsg.gettousername());
system.out.print(textmsg.tostring());
system.out.print(returnxml.tostring());
pw.println(returnxml);
}
private message getmessage(string xml){
xstream xstream = new xstream(new domdriver());
xstream.alias("xml", message.class);
xstream.aliasfield("tousername", message.class, "tousername");
xstream.aliasfield("fromusername", message.class, "fromusername");
xstream.aliasfield("createtime", message.class, "createtime");
xstream.aliasfield("msgtype", message.class, "messagetype");
xstream.aliasfield("content", message.class, "content");
xstream.aliasfield("msgid", message.class, "msgid");
message message = (message)xstream.fromxml(xml);
return message;
}
private string getreplymessage(string replymsg,string tousername,string fromusername){
replymessage we = new replymessage();
we.setmessagetype("text");
we.setfuncflag("0");
we.setcreatetime(new long(new date().gettime()).tostring());
we.setcontent(replymsg);
we.settousername(tousername);
we.setfromusername(fromusername);
xstream xstream = new xstream(new domdriver());
xstream.alias("xml", replymessage.class);
xstream.aliasfield("tousername", replymessage.class, "tousername");
xstream.aliasfield("fromusername", replymessage.class, "fromusername");
xstream.aliasfield("createtime", replymessage.class, "createtime");
xstream.aliasfield("msgtype", replymessage.class, "messagetype");
xstream.aliasfield("content", replymessage.class, "content");
xstream.aliasfield("funcflag", replymessage.class, "funcflag");
string xml =xstream.toxml(we);
return xml;
}
}
===========================================================================
package com.eiyoung.wechat.web.utils;
/**
* created with intellij idea.
* user: sb
* date: 8/1/13
* time: 10:37 pm
* to change this template use file | settings | file templates.
*/
public class replymessage {
private string funcflag;//消息编号
private string fromusername;//发送人
private string tousername;//接收人
private string content;//内容
private string messagetype;//消息类型
private string createtime;//创建日期
public string getcontent() {
return content;
}
public void setcontent(string content) {
this.content = content;
}
public string getmessagetype() {
return messagetype;
}
public void setmessagetype(string messagetype) {
this.messagetype = messagetype;
}
public string getfromusername() {
return fromusername;
}
public void setfromusername(string fromusername) {
this.fromusername = fromusername;
}
public string gettousername() {
return tousername;
}
public void settousername(string tousername) {
this.tousername = tousername;
}
public string getcreatetime() {
return createtime;
}
public void setcreatetime(string createtime) {
this.createtime = createtime;
}
public string getfuncflag() {
return funcflag;
}
public void setfuncflag(string funcflag) {
funcflag = funcflag;
}
public string tostring(){
return "createtime:"+getcreatetime()+"\\ntousername:"+gettousername()+"\\n fromusername:"+getfromusername()+"\\nmessagetype:"+getmessagetype()+"\\ncontent:"+getcontent();
}
}
===============================================================================
package com.eiyoung.wechat.web.utils;
import java.util.date;
/**
* created with intellij idea.
* user: sb
* date: 8/1/13
* time: 10:37 pm
* to change this template use file | settings | file templates.
*/
public class message {
private string msgid;//消息编号
private string fromusername;//发送人
private string tousername;//接收人
private string content;//内容
private string messagetype;//消息类型
private string createtime;//创建日期
public string getcontent() {
return content;
}
public void setcontent(string content) {
this.content = content;
}
public string getmessagetype() {
return messagetype;
}
public void setmessagetype(string messagetype) {
this.messagetype = messagetype;
}
public string getfromusername() {
return fromusername;
}
public void setfromusername(string fromusername) {
this.fromusername = fromusername;
}
public string gettousername() {
return tousername;
}
public void settousername(string tousername) {
this.tousername = tousername;
}
public string getcreatetime() {
return createtime;
}
public void setcreatetime(string createtime) {
this.createtime = createtime;
}
public string getmsgid() {
return msgid;
}
public void setmsgid(string msgid) {
this.msgid = msgid;
}
public string tostring(){
return "msgid:"+getmsgid()+"\\ncreatetime:"+getcreatetime()+"\\ntousername:"+gettousername()+"\\n fromusername:"+getfromusername()+"\\nmessagetype:"+getmessagetype()+"\\ncontent:"+getcontent();
}
}
==============================================================================
maven
<dependencies>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>servlet-api</artifactid>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupid>joda-time</groupid>
<artifactid>joda-time</artifactid>
<version>2.2</version>
</dependency>
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-io</artifactid>
<version>1.3.2</version>
</dependency>
<dependency>
<groupid>com.thoughtworks.xstream</groupid>
<artifactid>xstream</artifactid>
<version>1.4.3</version>
</dependency>
以上就是分享java版微信机器人的实现方法的详细内容。