h5 java实现微信分享_JAVA H5微信分享

该博客主要介绍了如何使用JAVA实现H5页面在微信中分享的签名过程,包括获取access_token、jsapi_ticket的方法,以及如何构造并计算签名的详细步骤。通过示例代码展示了关键函数的实现,包括HttpClient的使用。
摘要由CSDN通过智能技术生成

package com.unionpay.acp.sdk;

import java.util.UUID;

import java.util.Map;

import java.util.HashMap;

import java.util.Formatter;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.io.UnsupportedEncodingException;

import net.sf.json.JSONObject;

import com.xx.common.config.Global;

import com.unionpay.acp.sdk.HttpClient;/**

* 微信分享签名

**/

public classSign {public static voidmain(String[] args) {//String jsapi_ticket = "kgt8ON7yVITDhtdwci0qecRXz9HyXuyXgIslzmd3ri0s5EnLrVZZfrPJotRfKWQ-TTuQ2ZMAK86GUlWF1PHhcg";

};public static MapgetResult(){//String jsapi_ticket = getTicket();//注意 URL 一定要动态获取,不能 hardcode

String url = "http://www.xx.com/operation/mybusi/barcode";//String url = Global.getConfig("wx.shareUrl");

Map ret =sign(getTicket(), url);

ret.put("appId", getAppId());returnret;

}private staticString getAppId(){return Global.getConfig("wx.appid");

}private staticString getToken(){

String accessToken= "";

String url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+getAppId()+"&secret="+Global.getConfig("wx.secret");

HttpClient hc= new HttpClient(url, 30000, 30000);try{int status = hc.sendGet("utf-8");if (200 ==status) {

String resultString=hc.getResult();if (null != resultString && !"".equals(resultString)) {

JSONObject json=JSONObject.fromObject(resultString);if (null==json.get("errcode")){

accessToken= json.get("access_token").toString();

}else{

System.out.println(resultString);

}

}

}else{

System.out.println("返回http状态码["+status+"],请检查请求报文或者请求地址是否正确");

}

}catch(Exception e) {

e.printStackTrace();

}returnaccessToken;

}private staticString getTicket(){

String ticket= "";

String url= "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+getToken()+"&type=jsapi";

HttpClient hc= new HttpClient(url, 30000, 30000);try{int status = hc.sendGet("utf-8");if (200 ==status) {

String resultString=hc.getResult();if (null != resultString && !"".equals(resultString)) {

JSONObject json=JSONObject.fromObject(resultString);if ("0".equals(json.getString("errcode"))){

ticket= json.getString("ticket");

}else{

System.out.println(resultString);

}

}

}else{

System.out.println("返回http状态码["+status+"],请检查请求报文或者请求地址是否正确");

}

}catch(Exception e) {

e.printStackTrace();

}returnticket;

}public static Mapsign(String jsapi_ticket, String url) {

Map ret = new HashMap();

String nonce_str=create_nonce_str();

String timestamp=create_timestamp();

String string1;

String signature= "";//注意这里参数名必须全部小写,且必须有序

string1 = "jsapi_ticket=" + jsapi_ticket +

"&noncestr=" + nonce_str +

"&timestamp=" + timestamp +

"&url=" +url;

System.out.println(string1);try{

MessageDigest crypt= MessageDigest.getInstance("SHA-1");

crypt.reset();

crypt.update(string1.getBytes("UTF-8"));

signature=byteToHex(crypt.digest());

}catch(NoSuchAlgorithmException e)

{

e.printStackTrace();

}catch(UnsupportedEncodingException e)

{

e.printStackTrace();

}

ret.put("url", url);

ret.put("jsapi_ticket", jsapi_ticket);

ret.put("nonceStr", nonce_str);

ret.put("timestamp", timestamp);

ret.put("signature", signature);returnret;

}private static String byteToHex(final byte[] hash) {

Formatter formatter= newFormatter();for (byteb : hash)

{

formatter.format("%02x", b);

}

String result=formatter.toString();

formatter.close();returnresult;

}private staticString create_nonce_str() {returnUUID.randomUUID().toString();

}private staticString create_timestamp() {return Long.toString(System.currentTimeMillis() / 1000);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值