JavaWeb发送信息到微信公众平台的企业号

2023-12-04 01:48

本文主要是介绍JavaWeb发送信息到微信公众平台的企业号,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先到微信公众平台申请微信企业号: https://qy.weixin.qq.com
申请后需要在管理平台做如下几个步骤:
1、在通讯录里添加一个成员并使这个成员关注这个微信企业号;
2、在应用中心里新建一个应用并记录appid;
3、在设置里的权限管理中新建管理组;


新建一个JavaWeb工程并导入如下几个jar文件:
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.jar

下载jquery-2.1.4.min.js文件并添加到JavaWeb工程中。

JavaWeb工程的代码如下:

AccessToken.java文件内容如下:

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;
}

}


Text.java文件内容如下:

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;
}

}


MsgTypeAndDataFormat.java文件内容如下:

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;
}

}


WeixinCpServlet.java文件内容如下:

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();
}

}


web.xml文件内容如下:

<?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>


index.html文件内容如下:

<!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>



【[color=blue]注[/color]】:附件中是可运行源代码, :arrow:

这篇关于JavaWeb发送信息到微信公众平台的企业号的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/451659

相关文章

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.