加入收藏在线咨询
位置:主页 >

万趣娱乐:微信公共平台使用示例(一)网址接入示例代码

作者:dudu时间:11月23日浏览:


?package com.chaowen;

import java.io.IOException;

import java.io.PrintWriter;import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;import java.util.Arrays;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/奸淫 Servlet implementation class WeiXi万趣娱乐nServlet

*/ public class WeiXinServlet extends HttpServlet {

private static final long serialVersionUID = 1L;public static final String Token = "token";

protected void doGet(HttpServletRequest request, HttpSer万趣娱乐vletResponse response) throws ServletException, IOException {

this.doPost(request, response);}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//response.setContentType("text/xml;charset=utf-8");

response.setCharacterEncoding("UTF-8");PrintWriterout=response.getWriter();

String signature = request.getParameter("signature");

String timestamp = request.getParameter("timestamp");String nonce = request.getParameter("nonce");

String[] ArrTmp = { Token, timestamp, nonce };Arrays.sort(ArrTmp);

StringBuffer sb = new StringBuffer();for (int i = 0; i

sb.append(ArrTmp[i]);}

String pwd = Encrypt(sb.toString());String echostr = request.getParameter("echostr");

System.out.pri万趣娱乐ntln("pwd=="+pwd);System.out.println("echostr=="+echostr);

if(pwd.equals(signature)){if(!"".equals(echostr) && echostr != null){

response.getWriter().print(echostr);}

}

out.flush();out.close();

}

public String Encrypt(String strSrc) {MessageDigest md = null;

String strDes = null;

byte[] bt = strSrc.getBytes();try {

md = MessageDigest.getInstance("SHA-1");md.update(bt);

strDes = bytes2Hex(md.digest()); //to HexString} catch (NoSuchAlgorithmException e) {

System.out.println("Invalid algorithm.");return null;

}return strDe万趣娱乐s;

}

public String bytes2Hex(byte[] bts) {String des = "";

String tmp = null;for (int i = 0; i

tmp = (Integer.toHexString(bts[i] & 0xFF));if (tmp.length() == 1) {

des += "0";}

des +万趣娱乐= tmp;}

return des;}

}

电话:13866999966
联系人:王经理
Q Q:88996699
邮箱:admin@dede58.com
地址:中国XX省XX市XX路XX号