【Java开发】Java实现企业微信消息推送,通过应用发送

一、可能需要的依赖

之前导依赖的时候没有特别标记,现在分不清哪个是哪个了,应该就在这些里面

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.3</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.23</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

二、必须的信息

需要企业的ID号和密钥号,登录企业微信管理网页进行查看
在这里插入图片描述

在这里插入图片描述
还需要企业的ID号

在这里插入图片描述

三、效果展示

在这里插入图片描述

输如需要发送的消息

在这里插入图片描述

获取返回值
在这里插入图片描述

企业微信收到消息
在这里插入图片描述

四、具体代码

如果觉得不错就点个赞吧!!

1、这是主要实现的类

package TEST2;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Service
public class SendMsg {

    private static CloseableHttpClient httpClient;
    private static HttpPost httpPost;//用于提交登陆数据
    private static HttpGet httpGet;//用于获得登录后的页面
    //CorpID  企业ID
    //AGENTID 应用的ID
    //Secret 应用的ID对应的密钥
    public static final String CONTENT_TYPE = "Content-Type";
    public static final Integer AGENTID = 1000002;
    public static final String CORPID = "wwf38c66706451ab6d";
    public static final String CORPSECRET = "S1-z2PjSX0BSUn8U9rXjWqoo6Ix56lqaH5JD0gAf63Q";
    static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
    private static Gson gson = new Gson();

    /**
     *
     * @param toUser 用户的ID 格式"UserID1|UserID2|UserID3"
     * @param contentValue 推送消息内容
     * @throws IOException
     */
    public static void sendTextMesg(String toUser, String contentValue) throws IOException {
        String token = getToken(CORPID,CORPSECRET);
        String postData = createPostData(toUser, "text",AGENTID, "content", contentValue);
        String response = post("utf-8", CONTENT_TYPE,"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=", postData, token);
        System.out.println("获取到的token======>" + token);
        System.out.println("请求数据======>" + postData);
        System.out.println("发送微信的响应数据======>" + response);
    }
    public static String getToken(String corpId, String corpSecret) throws IOException {
        String resp = toAuth( "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + corpSecret);//拼接字符串得到url
        Map<String, Object> map = gson.fromJson(resp,
                new TypeToken<Map<String, Object>>() {
                }.getType());
        System.out.println(map);
        return map.get("access_token").toString();
    }

    protected static String toAuth(String Get_Token_Url) throws IOException {

        httpClient = HttpClients.createDefault();
        httpGet = new HttpGet(Get_Token_Url);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        System.out.println(response.toString());
        String resp;
        try {
            HttpEntity entity = response.getEntity();
            System.out.println(response.getAllHeaders());
            resp = EntityUtils.toString(entity, "utf-8");
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }

        return resp;
    }
    private static String createPostData(String touser, String msgtype, int agent_id, String contentKey, String contentValue) {
        Map<String,Object> weChatData = new HashMap<>();
        weChatData.put("touser",touser);
        weChatData.put("agentid",agent_id);
        weChatData.put("msgtype",msgtype);
        Map<Object, Object> content = new HashMap<Object, Object>();
        content.put(contentKey, contentValue + "\n--------\n" + df.format(new Date()));
        weChatData.put("text",content);
        System.out.println(gson.toJson(weChatData));
        return gson.toJson(weChatData);
    }

    private static String post(String charset, String contentType, String url, String data, String token) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        httpPost = new HttpPost(url + token);
        httpPost.setHeader(CONTENT_TYPE, contentType);
        httpPost.setEntity(new StringEntity(data, charset));
        CloseableHttpResponse response = httpclient.execute(httpPost);
        String resp;
        try {
            HttpEntity entity = response.getEntity();
            resp = EntityUtils.toString(entity, charset);
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
        return resp;
    }
}

2、执行的类

package TEST2;

import java.io.IOException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws IOException {
        String user = "xxxx";
        Scanner scan = new Scanner(System.in);
        String content = scan.nextLine();
//        String content = "你好呀~";
        SendMsg. sendTextMesg(user, content);
    }
}

  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Why_so?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值