java后端判斷用戶是否關注公眾號


/**
 * 判斷用戶是否關注了公眾號
 * @param openid
 * @return
 */
public static boolean judgeIsFollow(String openid){
    int subscribe = 0;
//        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+token+"&openid="+openid+"&lang=zh_CN";
    try {  
//        String token = getTicket();
        String token = "";
        if (StringUtil.isEmpty(token)) {
            token = getAccess_token();
        }
        String url = String.format(Constant.GzGzhUrl, token, openid);
        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);  
        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);  
        subscribe = demoJson.getInt("subscribe");

        is.close();  
    } catch (Exception e) {  
        e.printStackTrace();  
    }
    return 1==subscribe?true:false;
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM