Java微信公众号开发--开发环境的搭建

文章是根据本人根据慕课网的视频教程自己弄了一次,亲测成功。
1、首先在eclipse创建一个web工程,tomcat启动后可以通过访问index.jsp是否成功。
2、下载一个公网映射工具“grnok.exe”,接下来参考http://www.tunnel.mobi/里面的方法。下载ngrok.cfg文件,通过cmd.exe进行启动grnok。
输入dos命令“ngrok -config ngrok.cfg -subdomain weixin 7001”,其中weixin 自定义名称,7001为tomcat端口。执行后会显示如下图。
<img src="https://img-blog.csdn.net/20151005094736722?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

<span style="font-family: Arial, Helvetica, sans-serif;">3、在web工程中新建一个servlet来接收公众号服务器发来的请求信息,参考公众号的“开发者中心=》接入指南”。其中代码参考如下:</span>
代码1
public class WeixinServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
<span style="white-space:pre">			</span>throws ServletException, IOException {


<span style="white-space:pre">		</span>String signature = request.getParameter("signature");
<span style="white-space:pre">		</span>String timestamp = request.getParameter("timestamp");
<span style="white-space:pre">		</span>String nonce = request.getParameter("nonce");
<span style="white-space:pre">		</span>String echostr = request.getParameter("echostr");


<span style="white-space:pre">		</span>PrintWriter out = response.getWriter();
<span style="white-space:pre">		</span>if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
<span style="white-space:pre">			</span>out.print(echostr);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>out.close();
<span style="white-space:pre">	</span>}

}
代码2 检验公众号请求工具类
public class CheckUtil {
	
	private static final String token = "aineko";

	public static boolean checkSignature(String signature, String timestamp,
			String nonce) {
		
		String[] arr = new String[]{token,timestamp,nonce};
		
		//排序
		Arrays.sort(arr);
		
		//生成字符串
		StringBuffer content = new StringBuffer();
		for (int i = 0; i < arr.length; i++) {
			content.append(arr[i]);
		}
		
		//SHA1加密
		String temp = SHA1.getSha1(content.toString());
		
		return temp.equals(signature);
	}

}
代码3  SHA1加密方法
public class SHA1 {

public static String getSha1(String str) {
<span style="white-space:pre">		</span>if (null == str || 0 == str.length()) {
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
<span style="white-space:pre">				</span>'a', 'b', 'c', 'd', 'e', 'f' };
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
<span style="white-space:pre">			</span>mdTemp.update(str.getBytes("UTF-8"));


<span style="white-space:pre">			</span>byte[] md = mdTemp.digest();
<span style="white-space:pre">			</span>int j = md.length;
<span style="white-space:pre">			</span>char[] buf = new char[j * 2];
<span style="white-space:pre">			</span>int k = 0;
<span style="white-space:pre">			</span>for (int i = 0; i < j; i++) {
<span style="white-space:pre">				</span>byte byte0 = md[i];
<span style="white-space:pre">				</span>buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
<span style="white-space:pre">				</span>buf[k++] = hexDigits[byte0 & 0xf];
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>return new String(buf);
<span style="white-space:pre">		</span>} catch (NoSuchAlgorithmException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>} catch (UnsupportedEncodingException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">			</span>return null;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
}
代码4 web.xml 配置servlet
 <servlet>
    <servlet-name>WeixinServlet</servlet-name>
    <servlet-class>com.weixin.servlet.WeixinServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>WeixinServlet</servlet-name>
    <url-pattern>/wx.do</url-pattern>
  </servlet-mapping><span style="white-space:pre">	</span>

4、然后在开发者中心中配置,然后提交,如图




完成以上5步,Java微信公众号开发--开发环境的搭建 完成。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值