最近在研究微信公众号的开发


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


import javax.xml.ws.WebServiceException;


import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;





public class WechatUtils {
private static org.apache.log4j.Logger log = LogManager.getLogger(WechatUtils.class);



/**
     * 获取请求用户信息的access_token
     *
     * @param code
     * @return
* @throws Exception 
     */
public static WechatAccessTokenModel getAccessToken(String code,String appid,String secret) throws Exception {
        if (StringUtils.isBlank(code)) {
        log.error("code is null");
            throw new WebServiceException("code参数不对");
        }
        try {
            String url = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
            appid, secret, code);
            String accessTokenRes = HttpClientGetPostData.getInstance().getData(url);
            return JsonUtils.jsonToPojo(accessTokenRes, WechatAccessTokenModel.class);
        } catch (Exception e) {
        log.error("fail to request wechat access token. [error={}]", e);
        throw new WebServiceException("获取accessToken失败");
          
        }
    }

/************************************************************************************/

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 getTicket(String access_token) {  
    String ticket = null;  
    String url = String.format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi", access_token);//这个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.parseObject(message);  
        System.out.println("JSON字符串:"+demoJson);  
        ticket = demoJson.getString("ticket");  
        is.close();  
    } catch (Exception e) {  
            e.printStackTrace();  
    }  
    return ticket;  
}  



public static String getAccessToken(String appid,String secret) {  
    String access_token = "";  
    String grant_type = "client_credential";//获取access_token填写client_credential   
    //这个url链接地址和参数皆不能变  
    String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s"
       , grant_type,appid, 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 = JSON.parseObject(message);
        System.out.println("JSON字符串:"+message);  
        access_token = demoJson.getString("access_token");  
        is.close();  
    } catch (Exception e) {  
            e.printStackTrace();  
    }  
    return access_token;  
}  


}

猜你喜欢

转载自blog.csdn.net/weixin_39225655/article/details/80433383