最近在网上搜了一下通过java给微信企业推送消息的demo乱七八糟,就决定写一个简洁的demo出来,希望有同样问题的不要走冤枉路,需要一个json处理的jar(fastjson.jar) //==========================
//======================================工具类================================================== import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class HttpRequest { private static Log log = LogFactory.getLog(HttpRequest.class); /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return URL 所代表远程资源的响应结果 */ public static String sendGet(String url) { StringBuffer result = new StringBuffer(); BufferedReader in = null; try { String urlNameString = url; URL realUrl = new URL(urlNameString); URLConnection connection = realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { log.error("发送请求" + url + "失败", e); } finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result.toString(); } public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; StringBuffer result = new StringBuffer(); try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setDoOutput(true); conn.setDoInput(true); out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8")); out.print(param); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { log.error("发送post请求" + url + "失败", e); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result.toString(); } //======================================发送消息类================================================== import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; public class WeChat { /** * 微信企业号发送信息 */ public static String setMsg(String corpid, String corpsecret, int agentid, String touser, String content) throws Exception { String s = HttpRequest .sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret); if (s == null) { return null; } JSONObject parse = (JSONObject) JSON.parse(s); if (parse == null) { return null; } String object = String.valueOf(parse.get("access_token")); if (object == null) { return null; } Mapfastjson-1.1.34.jar fastjson-1.1.34.jarmsg = new HashMap<>(); msg.put("touser", touser); msg.put("msgtype", "text"); msg.put("agentid", agentid); Map text = new HashMap<>(); text.put("content", content); msg.put("text", text); String json = JSON.toJSONString(msg, SerializerFeature.DisableCircularReferenceDetect); return HttpRequest.sendPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + object + "", json); } //====================测试方法 public static void main(String[] args) { try { System.out.println(setMsg("ww2eca777777777777", "gDXXall0pvrrEJrdmwxTsVDv45RIbHs4J_8888888", 1000002, "cp", "你好")); } catch (Exception e) { e.printStackTrace(); } } }