A. 在SAE上部署的java微信应用,如何和微信接口
代码如下:
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.math.BigInteger;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.util.Arrays;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
{
=1L;
privatestaticfinalStringTOKEN="sinavoice";
/**
*@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)
*/
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
PrintWriterout=response.getWriter();
Stringtimestamp=request.getParameter("timestamp");
Stringnonce=request.getParameter("nonce");
Stringsignature=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();
}
/**
*@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)
*/
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
PrintWriterout=response.getWriter();
//StringBufferpostText=StreamOfFileUtils.StreamForString(request.getInputStream());
//log.debug(postText.toString());
out.flush();
out.close();
}
/**
*@param_signature
*@param_timestamp
*@param_nonce
*@return
*/
(String_signature,String_timestamp,String_nonce){
String[]ttn=newString[3];
ttn[0]=TOKEN;
ttn[1]=_timestamp;
ttn[2]=_nonce;
Arrays.sort(ttn);
StringpwdSignature=String.format("%s%s%s",ttn[0],ttn[1],ttn[2]);
StringsrcSignature=_signature;
try{
MessageDigestmd=MessageDigest.getInstance("SHA1");
md.update(pwdSignature.getBytes());
pwdSignature=newBigInteger(1,md.digest()).toString(16);
}catch(NoSuchAlgorithmExceptione){
}
returnsrcSignature==null?false:srcSignature.equals(pwdSignature);
}
}