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

猜你喜欢

转载自www.cnblogs.com/BobXie85/p/9724142.html