JavaWeb sends information to the enterprise account of the WeChat public platform

First, go to the WeChat public platform to apply for a WeChat enterprise account: https://qy.weixin.qq.com
After applying, you need to do the following steps on the management platform:
1. Add a member to the address book and make this member follow this WeChat enterprise 2.
Create a new application in the application center and record the appid;
3. Create a new management group in the permission management in the settings;


create a new JavaWeb project and import the following jar files:
commons-logging-1.1.1.jar, fastjson-1.2.7.jar,httpclient-4.5.1.jar,httpcore-4.4.3.jar,weixin-java-cp-1.3.1.jarDownload

jquery-2.1.4.min.js file and add it to JavaWeb under construction.

The code of the JavaWeb project is as follows: The

content of the AccessToken.java file is as follows:
package com.shihuan.pojo;

import java.io.Serializable;

public class AccessToken implements Serializable {

	private String access_token;
	private String expires_in;
	
	public AccessToken() {
		
	}

	public String getAccess_token() {
		return access_token;
	}

	public void setAccess_token(String accessToken) {
		access_token = accessToken;
	}

	public String getExpires_in() {
		return expires_in;
	}

	public void setExpires_in(String expiresIn) {
		expires_in = expiresIn;
	}
	
}


The contents of the Text.java file are as follows:
package com.shihuan.pojo;

import java.io.Serializable;

public class Text implements Serializable {

	private String content;

	public Text() {
		
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
}


The contents of the MsgTypeAndDataFormat.java file are as follows:
package com.shihuan.pojo;

import java.io.Serializable;

public class MsgTypeAndDataFormat implements Serializable {

	private String touser;
	private String toparty;
	private String totag;
	private String msgtype;
	private String agentid;
	private Text text;
	private String safe;
	
	public MsgTypeAndDataFormat() {
		
	}

	public String getTouser() {
		return touser;
	}

	public void setTouser(String touser) {
		this.touser = touser;
	}

	public String getToparty() {
		return toparty;
	}

	public void setToparty(String toparty) {
		this.toparty = toparty;
	}

	public String getTotag () {
		return totag;
	}

	public void setTotag(String totag) {
		this.totag = totag;
	}

	public String getMsgtype() {
		return msgtype;
	}

	public void setMsgtype(String msgtype) {
		this.msgtype = msgtype;
	}

	public String getAgentid() {
		return agentid;
	}

	public void setAgentid(String agentid) {
		this.agentid = agentid;
	}

	public Text getText() {
		return text;
	}

	public void setText(Text text) {
		this.text = text;
	}

	public String getSafe() {
		return safe;
	}

	public void setSafe(String safe) {
		this.safe = safe;
	}
	
}


The content of the WeixinCpServlet.java file is as follows:
package com.shihuan.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.shihuan.pojo.AccessToken;
import com.shihuan.pojo.MsgTypeAndDataFormat;
import com.shihuan.pojo.Text;

public class WeixinCpServlet extends HttpServlet {

	protected void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse) throws ServletException, IOException {
		System.out.println("doGet...");
		paramHttpServletResponse.getWriter().write("WeixinCp doGet!");
		paramHttpServletResponse.flushBuffer ();
		System.out.println("doGet...");
	}

	protected void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse) throws ServletException, IOException {
		System.out.println("doPost...");
		
		String corpid = paramHttpServletRequest.getParameter("corpid");
		String corpsecret = paramHttpServletRequest.getParameter("corpsecret");
		StringBuilder sb = new StringBuilder();
		sb.append("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=").append(corpid).append("&corpsecret=").append(corpsecret);
		String getTokenUrl = sb.toString();
		HttpPost getTokenUrlPost = new HttpPost(getTokenUrl);
		DefaultHttpClient getTokenUrlClient = new DefaultHttpClient();
		HttpResponse getTokenUrlResponse = getTokenUrlClient.execute(getTokenUrlPost);
		AccessToken atentry = new AccessToken();
		if (getTokenUrlResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        	HttpEntity entity = getTokenUrlResponse.getEntity();
        	String jsontext = EntityUtils.toString(entity, "utf-8");
        	System.out.println(jsontext);
        	atentry = (AccessToken) JSON.parseObject(jsontext, AccessToken.class);
        }
		
		String access_token = atentry.getAccess_token();
		System.out.println(access_token);
		StringBuilder sbsend = new StringBuilder();
		sbsend.append("https://qyapi.weixin.qq.com/cgi-bin/message/send?body=0&access_token=").append(access_token);
		String url = sbsend.toString();
		
		String jsonbody = paramHttpServletRequest.getParameter("jsonbody");
		
		Text t = new Text();
		t.setContent(jsonbody);
		
		MsgTypeAndDataFormat m = new MsgTypeAndDataFormat();
		m.setAgentid("2");
		m.setMsgtype("text");
		m.setSafe("0");
		m.setText(t);
		m.setToparty("@all");
		m.setTotag("@all");
		m.setTouser("@all");
		
		String json = JSON.toJSONString(m);
		
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		
		StringEntity s = new StringEntity(json);
		s.setContentEncoding("UTF-8");
        s.setContentType("application/json");
        post.setEntity(s);
        
        HttpResponse res = client.execute(post);
        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        	HttpEntity entity = res.getEntity();
        	System.out.println(EntityUtils.toString(entity, "utf-8"));
        }
		
		System.out.println("doPost...");
	}

	public void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws ServletException,IOException {
		System.out.println("WeixinCpService...");
		super.service(paramServletRequest, paramServletResponse);
		paramServletResponse.getWriter().write("WeixinCp service!");
		System.out.println("WeixinCpService...");
	}

	public void init(ServletConfig paramServletConfig) throws ServletException {
		super.init(paramServletConfig);
		System.out.println("WeixinCpServlet...");
	}

	public void destroy() {
		System.out.println("WeixinServlet[destroy]...");
		super.destroy();
	}

}


The content of the web.xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<display-name>WeixinCp</display-name>
	
	<servlet>
		<servlet-name>WeixinCpServlet</servlet-name>
		<servlet-class>com.shihuan.servlet.WeixinCpServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>WeixinCpServlet</servlet-name>
		<url-pattern>/weixincp</url-pattern>
	</servlet-mapping>



	<session-config>
		<session-timeout>60</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>

</web-app>


The content of the index.html file is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>weixincp servlet</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
	<script type="text/javascript">
		function sendWxMsg(){
			var url = "weixincp";
			$.post(url, {corpid:$("#corpid").val(),corpsecret:$("#corpsecret").val(),jsonbody:$("#jsonbody").val()});
		}
	</script>
  </head>
  
  <body>
    <form name="frm_data" id="frm_data" method="POST" action="weixincp">
	  corpid: <input type="text" id="corpid" name="corpid" value="" size="80px" /><br />
	  corpsecret: <input type="text" id="corpsecret" name="corpsecret" value="" size="80px" /><br />
	  content: <input type="text" id="jsonbody" name="jsonbody" value="" size="80px" /><br /><br />
	  <input type="button" id="sendbtn" name="sendbtn" value="sendWxMsg" onclick="sendWxMsg();" />
	</form>
  </body>
</html>



[ Note ]: The attachment is the executable source code,

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326758596&siteId=291194637