在SAE上部署的JAVA微信应用,如何和微信接口
1个回答
展开全部
代码如下:
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
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;
public class CoreServelt extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String TOKEN = "sinavoice" ;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter() ;
String timestamp = request.getParameter("timestamp") ;
String nonce = request.getParameter("nonce") ;
String signature = request.getParameter("signature") ;
if(this.validataSignature(signature, timestamp, nonce)){
out.println(request.getParameter("echostr")) ;
}
else{
response.setStatus(HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED);
}
out.flush() ;
out.close() ;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter() ;
// StringBuffer postText = StreamOfFileUtils.StreamForString(request.getInputStream()) ;
// log.debug(postText.toString());
out.flush();
out.close();
}
/**
* @param _signature
* @param _timestamp
* @param _nonce
* @return
*/
private boolean validataSignature(String _signature , String _timestamp , String _nonce){
String[] ttn = new String[3] ;
ttn[0]=TOKEN ;
ttn[1]=_timestamp ;
ttn[2]=_nonce ;
Arrays.sort(ttn);
String pwdSignature = String.format("%s%s%s", ttn[0],ttn[1],ttn[2]) ;
String srcSignature = _signature ;
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(pwdSignature.getBytes());
pwdSignature = new BigInteger(1, md.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
}
return srcSignature==null?false:srcSignature.equals(pwdSignature) ;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询