微信公众平台的接口url

微信公众平台的接口url

  1. public class WeixinAction extends ActionSupport{  
  2.     private String signature;  
  3.     private String timestamp;  
  4.     private String nonce;  
  5.     private String echostr;  
  6.     private String token;  
  7.       
  8.     ActionContext context = ActionContext.getContext();  
  9.     private HttpServletResponse response = ServletActionContext.getResponse();  
  10.     public void weiXinInfo() throws Exception{  
  11.         //1. 将token、timestamp、nonce三个参数进行字典序排序  
  12.         token = “weixin”;  
  13.         String[] str = {token,timestamp,nonce};  
  14.         for(int i=0;i<str.length-1;i++){  
  15.             for (int j = i + 1; j < str.length; j++) {    
  16.                 if(str[i].compareTo(str[j])>0){//字符串比较用compareTo方法    
  17.                     String temp = str[i];    
  18.                     str[i] = str[j];    
  19.                     str[j] = temp;    
  20.                 }     
  21.             }    
  22.         }  
  23.         StringBuilder sb = new StringBuilder();  
  24.         for(String str1 : str){  
  25.             sb.append(str1);  
  26.         }  
  27.         String s = sb.toString();  
  28.         response.setContentType(“text/html”);  
  29.         response.setCharacterEncoding(“UTF-8”);  
  30.         PrintWriter out= response.getWriter();  
  31.         if(SHA1Util.encodeBySHA(s).equals(signature)){  
  32.             //LogUtil.logger.info(“echo”+echostr);  
  33.             out.write(echostr);  
  34.         }else{  
  35.             //LogUtil.logger.info(“fail”+echostr);  
  36.             out.write(“false”);  
  37.         }  
  38.         out.flush();   
  39.         out.close();  
  40.     }  
  41.     public String getToken() {  
  42.         return token;  
  43.     }  
  44.     public void setToken(String token) {  
  45.         this.token = token;  
  46.     }  
  47.     public String getSignature() {  
  48.         return signature;  
  49.     }  
  50.     public void setSignature(String signature) {  
  51.         this.signature = signature;  
  52.     }  
  53.     public String getTimestamp() {  
  54.         return timestamp;  
  55.     }  
  56.     public void setTimestamp(String timestamp) {  
  57.         this.timestamp = timestamp;  
  58.     }  
  59.     public String getNonce() {  
  60.         return nonce;  
  61.     }  
  62.     public void setNonce(String nonce) {  
  63.         this.nonce = nonce;  
  64.     }  
  65.     public String getEchostr() {  
  66.         return echostr;  
  67.     }  
  68.     public void setEchostr(String echostr) {  
  69.         this.echostr = echostr;  
  70.     }  
  71. }  

[java]

  1. //进行SHA-1加密  
  2. public class SHA1Util {  
  3.   
  4.   
  5.     /** 
  6.     * 转换字节数组为十六进制字符串 
  7.     * @param b 
  8.     * @return 
  9.     */  
  10.     private final static String[] hexDigits = {“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”“a”“b”“c”“d”“e”“f”};  
  11.    public static String byteArrayToHexString(byte[] b){    
  12.        StringBuffer stringBuffer = new StringBuffer();    
  13.        for (int i = 0; i < b.length; i++){    
  14.            stringBuffer.append(byteToHexString(b[i]));    
  15.        }    
  16.        return stringBuffer.toString();    
  17.    }    
  18.      
  19.    /** 
  20.     * 将一个字节转化成十六进制形式的字符串 
  21.     * @param b 
  22.     * @return 
  23.     */  
  24.    public static String byteToHexString(byte b){    
  25.        int n = b;    
  26.        if (n < 0) {  
  27.            n = 256 + n;    
  28.        }  
  29.        int d1 = n / 16;    
  30.        int d2 = n % 16;    
  31.        return hexDigits[d1] + hexDigits[d2];    
  32.    }   
  33.      
  34.    /**      
  35.     * 字符串SHA-1加密 
  36.     * @param string 
  37.     * @return 
  38.     */  
  39.    public static String encodeBySHA(String string){  
  40.        if(string != null && !string.isEmpty()){  
  41.            try {  
  42.                //创建具有指定算法名称的信息摘要    
  43.                MessageDigest messageDigest = MessageDigest.getInstance(“SHA-1”);  
  44.                //使用指定的字节数组对摘要进行最后更新,然后完成摘要计算    
  45.                byte[] bytes = messageDigest.digest(string.getBytes());  
  46.                //将得到的字节数组变成字符串返回    
  47.                string = byteArrayToHexString(bytes);    
  48.            } catch (Exception e) {  
  49.                // TODO Auto-generated catch block  
  50.                e.printStackTrace();  
  51.            }  
  52.        }  
  53.        return string;  
  54.    }  
  55. }  
  56.  
  57. httpClient请求绕过验证
  58. HttpClient httpclient = new DefaultHttpClient();
    httpclient = HttpUtil.wrapClient(httpclient);
    /**
    * 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”异常
    * 不用导入SSL证书
    *
    * @author shipengzhi(shipengzhi@sogou-inc.com)
    *
    */
    public static org.apache.http.client.HttpClient wrapClient(org.apache.http.client.HttpClient base) {
    try {
    SSLContext ctx = SSLContext.getInstance(“TLS”);
    X509TrustManager tm = new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
    return null;
    }

    @SuppressWarnings(“unused”)
    public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
    }

    @SuppressWarnings(“unused”)
    public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
    }

    @Override
    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {

    }

    @Override
    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
    }
    };
    ctx.init(null, new TrustManager[] { tm }, null);
    SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme(“https”, 443, ssf));
    ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);
    return new DefaultHttpClient(mgr, base.getParams());
    } catch (Exception ex) {
    ex.printStackTrace();
    return null;
    }
    }

  59.  

标签