想好再写!!!
--------------------------------------------------------------------------------------------------------------------------------------
先贴下源码:(简单易懂)
package com.test.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import net.sf.json.JSONObject;//无法找到解释说明文档--缺少.zip文件
import java.io.InputStream;
import java.net.URL;
import java.net.HttpURLConnection;
public class airkiss {
public static String getAccessToken() {
String access_token = "";
String grant_type = "client_credential";//获取access_token填写client_credential
String appId="xxxxxxxxxxx";//第三方用户唯一凭证
String secret="x'x'x'x'x'x'x'x'x'x'x'x'x'x'x";//第三方用户唯一凭证密钥,即appsecret
//这个url链接地址和参数皆不能变
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+appId+"&secret="+secret;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = JSONObject.fromObject(message);
System.out.println("JSON字符串:"+demoJson);
access_token = demoJson.getString("access_token");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return access_token;
}
//********************************************************************************************//
public static String getTicket(String access_token) {
String ticket = null;
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token +"&type=jsapi";//这个url链接和参数不能变
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = JSONObject.fromObject(message);
System.out.println("jsapi字符串:"+demoJson);
ticket = demoJson.getString("ticket");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return ticket;
}
//********************************************************************************************//
public static String SHA1(String decript) {
try {
MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为 十六进制 数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
//********************************************************************************************//
public static String getnoncestr() {
String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串
return noncestr;
}
public static String gettimestamp() {
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳 当前时间
return timestamp;
}
//********************************************************************************************//
public static String times[] = {"0","0"};
public static int a = 0;
public static int b = 0;
public static int n = 0;
static String accessToken = null;
static String jsapi_ticket = null;
//********************************************************************************************//
public static String[] getSignatur() {
a = Integer.parseInt(times[1]);
b = Integer.parseInt(times[0]);
System.out.println("获取access时间:"+times[0]);
System.out.println("当前获取access:"+times[1]);
if(n == 0){//初始化获取参数
//1、获取AccessToken
accessToken = getAccessToken();
//2、获取Ticket
jsapi_ticket = getTicket(accessToken);
n = 1;
}
if(a - b > 7000){//定时获取参数
//1、获取AccessToken
String accessT = getAccessToken();
String jsapi_t = getTicket(accessT);
accessToken = accessT;
jsapi_ticket = jsapi_t;
times[0] = times[1];
}
//3、时间戳和随机字符串
String noncestr = getnoncestr();
String timestamp = gettimestamp();
times[1] = timestamp;
System.out.println("accessToken:"+accessToken+"\njsapi_ticket:"+jsapi_ticket+"\n时间戳:"+timestamp+"\n随机字符串:"+noncestr);
String url = "http://xxxxxxxx";
//4、获取url
/* if(n == 1)
{
url="http://xxxxxxxxxx";
}
if(n == 0) {
n = 1;
}*/
//5、将参数排序并拼接字符串
String str = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"×tamp="+timestamp+"&url="+url;
//6、将字符串进行sha1加密
String signature = SHA1(str);
System.out.println("参数:"+str+"\n签名:"+signature);
String[] sendstr = new String[3];
sendstr[0] = timestamp;
sendstr[1] = noncestr;
sendstr[2] = signature;
return sendstr;
}
}