public
static
String getSignature(String accessToken,String jsapi_ticket,String noncestr,String timestamp,String url){
System.out.println(
"accessToken:"
+accessToken+
"\njsapi_ticket:"
+jsapi_ticket+
"\n时间戳:"
+timestamp+
"\n随机字符串:"
+noncestr+
"\nURL:"
+url);
String str =
"jsapi_ticket="
+jsapi_ticket+
"&noncestr="
+noncestr+
"×tamp="
+timestamp+
"&url="
+url;
String signature =SHA1(str);
System.out.println(
"参数:"
+str+
"\n签名:"
+signature);
return
signature;
}
public
static
String getAccessToken() {
String wx_appid = getProperties(
"wx_appid"
);
String wx_appsecret = getProperties(
"wx_appsecret"
);
String access_token =
""
;
String grant_type =
"client_credential"
;
String AppId=wx_appid;
String secret=wx_appsecret;
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"
);
http.setRequestProperty(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty(
"sun.net.client.defaultConnectTimeout"
,
"30000"
);
System.setProperty(
"sun.net.client.defaultReadTimeout"
,
"30000"
);
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"
);
http.setRequestProperty(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty(
"sun.net.client.defaultConnectTimeout"
,
"30000"
);
System.setProperty(
"sun.net.client.defaultReadTimeout"
,
"30000"
);
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);
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();
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
""
;
}