微信公众平台的接口url
2016-04-25 00:00
483 查看
[java]
view plain
copy
public class WeixinAction extends ActionSupport{
private String signature;
private String timestamp;
private String nonce;
private String echostr;
private String token;
ActionContext context = ActionContext.getContext();
private HttpServletResponse response = ServletActionContext.getResponse();
public&
3ff0
nbsp;void weiXinInfo() throws Exception{
//1. 将token、timestamp、nonce三个参数进行字典序排序
token = "weixin";
String[] str = {token,timestamp,nonce};
for(int i=0;i<str.length-1;i++){
for (int j = i + 1; j < str.length; j++) {
if(str[i].compareTo(str[j])>0){//字符串比较用compareTo方法
String temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
StringBuilder sb = new StringBuilder();
for(String str1 : str){
sb.append(str1);
}
String s = sb.toString();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out= response.getWriter();
if(SHA1Util.encodeBySHA(s).equals(signature)){
//LogUtil.logger.info("echo"+echostr);
out.write(echostr);
}else{
//LogUtil.logger.info("fail"+echostr);
out.write("false");
}
out.flush();
out.close();
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getTimestamp() {
return timestamp;
3ff0
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getNonce() {
return nonce;
}
public void setNonce(String nonce) {
this.nonce = nonce;
}
public String getEchostr() {
return echostr;
}
public void setEchostr(String echostr) {
this.echostr = echostr;
}
}
[java]
view plain
copy
//进行SHA-1加密
public class SHA1Util {
/**
* 转换字节数组为十六进制字符串
* @param b
* @return
*/
private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
public static String byteArrayToHexString(byte[] b){
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < b.length; i++){
stringBuffer.append(byteToHexString(b[i]));
}
return stringBuffer.toString();
}
/**
* 将一个字节转化成十六进制形式的字符串
* @param b
* @return
*/
public static String byteToHexString(byte b){
int n = b;
if (n < 0) {
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
/**
* 字符串SHA-1加密
* @param string
* @return
*/
public static String encodeBySHA(String string){
if(string != null && !string.isEmpty()){
try {
//创建具有指定算法名称的信息摘要
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
//使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
byte[] bytes = messageDigest.digest(string.getBytes());
//将得到的字节数组变成字符串返回
string = byteArrayToHexString(bytes);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return string;
}
}
httpClient请求绕过验证
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;
}
}
view plain
copy
public class WeixinAction extends ActionSupport{
private String signature;
private String timestamp;
private String nonce;
private String echostr;
private String token;
ActionContext context = ActionContext.getContext();
private HttpServletResponse response = ServletActionContext.getResponse();
public&
3ff0
nbsp;void weiXinInfo() throws Exception{
//1. 将token、timestamp、nonce三个参数进行字典序排序
token = "weixin";
String[] str = {token,timestamp,nonce};
for(int i=0;i<str.length-1;i++){
for (int j = i + 1; j < str.length; j++) {
if(str[i].compareTo(str[j])>0){//字符串比较用compareTo方法
String temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
StringBuilder sb = new StringBuilder();
for(String str1 : str){
sb.append(str1);
}
String s = sb.toString();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out= response.getWriter();
if(SHA1Util.encodeBySHA(s).equals(signature)){
//LogUtil.logger.info("echo"+echostr);
out.write(echostr);
}else{
//LogUtil.logger.info("fail"+echostr);
out.write("false");
}
out.flush();
out.close();
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getTimestamp() {
return timestamp;
3ff0
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getNonce() {
return nonce;
}
public void setNonce(String nonce) {
this.nonce = nonce;
}
public String getEchostr() {
return echostr;
}
public void setEchostr(String echostr) {
this.echostr = echostr;
}
}
[java]
view plain
copy
//进行SHA-1加密
public class SHA1Util {
/**
* 转换字节数组为十六进制字符串
* @param b
* @return
*/
private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
public static String byteArrayToHexString(byte[] b){
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < b.length; i++){
stringBuffer.append(byteToHexString(b[i]));
}
return stringBuffer.toString();
}
/**
* 将一个字节转化成十六进制形式的字符串
* @param b
* @return
*/
public static String byteToHexString(byte b){
int n = b;
if (n < 0) {
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
/**
* 字符串SHA-1加密
* @param string
* @return
*/
public static String encodeBySHA(String string){
if(string != null && !string.isEmpty()){
try {
//创建具有指定算法名称的信息摘要
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
//使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
byte[] bytes = messageDigest.digest(string.getBytes());
//将得到的字节数组变成字符串返回
string = byteArrayToHexString(bytes);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return string;
}
}
httpClient请求绕过验证
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;
}
}
相关文章推荐
- 微信开发
- 微信文章排版
- 微信文章如何获得上万次转载?
- 微信分销系统源码定制开发
- 开通微信公众号流程目录(截图版)
- 开通微信公众号流程所需资料及时间
- 微信公众号认证流程
- 微信支付认证
- 微信支付认证所需资料
- 开通微信公众号开发者中心
- 分享到微信好友或朋友圈
- 2015年在微信热传的100+经典技术文章-读书笔记
- 基于NodeJS微信公众号
- java微信开发API解析(六)-综合运用(消息处理、自定义菜单响应处理、用户自动分组处理;包含源码)
- 分享到微信微博空间等第三方平台的JS代码
- 仿微信表情输入
- JEEWX微信企业号管家,开源免费,1.0版本发布
- 微信运营案例分析
- 微信开发
- 实现微信布局的四种方式()四