public static AccessTokenReturnMessage sendMessage(CorpSendBase message) { String SEND_MSG_URL = QyWxURLUtil.getMsgSendURL(); try { // 创建连接 URL url = new URL(SEND_MSG_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.connect(); // POST请求 JSONObject obj = JSONObject.fromObject(message); System.out.println("JsonObject:" + obj.toString()); // DataOutputStream out = new DataOutputStream( // connection.getOutputStream()); // out.writeBytes(obj.toString()); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "utf-8")); out.write(obj.toString()); out.flush(); out.close(); // 读取响应 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } System.out.println(sb); reader.close(); JSONObject jsonObj1 = JSONObject.fromObject(sb.toString()); AccessTokenReturnMessage p2; p2 = (AccessTokenReturnMessage) JSONObject.toBean(jsonObj1, AccessTokenReturnMessage.class); // 断开连接 connection.disconnect(); return p2; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
/** * 获取成员 请求说明 Https请求方式: GET * https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=lisi 参数说明 参数 必须 * 说明 access_token 是 调用接口凭证 userid 是 员工UserID。对应管理端的帐号 返回结果 { "errcode": 0, "errmsg": "ok", * "userid": "zhangsan", "name": "李四", "department": [1, 2], "position": "后台工程师", "mobile": * "15913215421", "email": "*****@*****.**", "weixinid": "lisifordev", "avatar": * "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TBicnHFlhiafvDwklOpZeXYQQ2icg/0", * "status": 1, "extattr": * {"attrs":[{"name":"爱好","value":"旅游"},{"name":"卡号","value":"1234567234"}]} } * * <p>参数 说明 errcode 返回码 errmsg 对返回码的文本描述内容 userid 员工UserID。对应管理端的帐号 name 成员名称 department * 成员所属部门id列表 position 职位信息 mobile 手机号码 email 邮箱 weixinid 微信号 avatar * 头像url。注:如果要获取小图将url最后的"/0"改成"/64"即可 status 关注状态: 1=已关注,2=已冻结,4=未关注 extattr 扩展属性 权限说明 * 管理员须拥有’获取成员’的接口权限,以及成员的查看权限。 * * @param acessToken * @param userId * @return * @throws Exception */ public static UserInfo getUserInfoFromUserID(String userId) throws Exception { System.out.println("getUserInfoFromUserID.userId" + userId); String queryUserURL = QyWxURLUtil.getQueryUserURL(); queryUserURL = MessageFormat.format(queryUserURL, 0, userId); System.out.println("getUserInfoFromUserID.queryUserURL" + queryUserURL); String returnMsg = HttpUtil.httpGet(queryUserURL); JSONObject jsonObj1 = JSONObject.fromObject(returnMsg); Map<String, Class> classMap = new HashMap<String, Class>(); classMap.put("extattr", UserInfoExtraAttr.class); classMap.put("attrs", Attr.class); UserInfo info = (UserInfo) JSONObject.toBean(jsonObj1, UserInfo.class, classMap); return info; }