本次的教程首要是对微信公家平台开拓者模式的讲授,收集上许多相同文章,但许多都让初学微信开拓的人一头雾水,以是总结本身的微信开拓履历,将微信开拓的整个进程体系的列出,并对首要代码举办讲授说明,让初学者尽快上手。
在阅读本文之前,应对微信公家平台的官方开拓文档有所相识,知道吸取和发送的都是xml名目标数据。其它,在做内容回覆时用到了图灵呆板人的api接口,这是一个天然说话理会的开放平台,可以帮我们办理整个微信开拓进程中最坚苦的题目,此处不多讲,下面会有其具体的挪用方法。
1.1 在登录微信官方平台之后,开启开拓者模式,此时必要我们填写url和token,所谓url就是我们本身处事器的接口,用WechatServlet.java来实现,相干表明已经在注释中声名,代码如下:
package demo.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import demo.process.WechatProcess;
/**
* 微信处事端收动员静接口
*
* @author pamchen-1
*
*/
public class WechatServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(“UTF-8”);
response.setCharacterEncoding(“UTF-8”);
/** 读取吸取到的xml动静 */
StringBuffer sb = new StringBuffer();
InputStream is = request.getInputStream();
InputStreamReader isr = new InputStreamReader(is, “UTF-8”);
BufferedReader br = new BufferedReader(isr);
String s = “”;
while ((s = br.readLine()) != null) {
sb.append(s);
}
String xml = sb.toString(); //次即为吸取到微信端发送过来的xml数据
String result = “”;
/** 判定是否是微信接入激活验证,只有初次接入验证时才会收到echostr参数,此时必要把它直接返回 */
String echostr = request.getParameter(“echostr”);
if (echostr != null && echostr.length() > 1) {
result = echostr;
} else {
//正常的微信处理赏罚流程
result = new WechatProcess().processWechatMag(xml);
}
try {
OutputStream os = response.getOutputStream();
os.write(result.getBytes(“UTF-8”));
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
1.2 响应的web.xml设置信息如下,在天生WechatServlet.java的同时,可自动天生web.xml中的设置。前面所提到的url处可以填写譬喻:http;//处事器地点/项目名/wechat.do
<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″
xmlns=””
xmlns:xsi=””
xsi:schemaLocation=”
“>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>WechatServlet</servlet-name>
<servlet-class>demo.servlet.WechatServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WechatServlet</servlet-name>
<url-pattern>/wechat.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.3 通过以上代码,我们已经实现了微信公家平台开拓的框架,即开通开拓者模式并乐成接入、吸取动静和发送动静这三个步调。