java微信报警,Java接入微信告警实现代码(Cat点评)

这个类主要用于微信接口的调用,包括获取授权响应、解析access_token、构造并发送微信消息。通过RestTemplate和HttpClient实现HTTP请求,使用Gson进行JSON解析。主要方法包括toAuth、getToken、createPostData和post,分别处理不同阶段的微信交互过程。
摘要由CSDN通过智能技术生成

package com.platform.monitor.common.util;

import com.google.common.reflect.TypeToken;

import com.google.gson.Gson;

import com.platform.monitor.vo.IacsUrlDataVo;

import com.platform.monitor.vo.IacsWeChatDataVo;

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.slf4j.LoggerFactory;

import org.springframework.web.client.RestTemplate;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

/**

* @ClassName: SendMsgUtils

* @Author: shenlele

* @Date: 2018/10/11 14:08

* @Description:

*/

public class SendWeChatUtils {

RestTemplate restTemplate = new RestTemplate();

private CloseableHttpClient httpClient;

private HttpPost httpPost;//用于提交登陆数据

private HttpGet httpGet;//用于获得登录后的页面

public static final String CONTENT_TYPE = "Content-Type";

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

private static Gson gson = new Gson();

/**

* 微信授权请求,GET类型,获取授权响应,用于其他方法截取token

*

* @param Get_Token_Url

* @return String 授权响应内容

* @throws IOException

*/

public String toAuth(String Get_Token_Url) throws IOException {

httpClient = HttpClients.createDefault();

httpGet = new HttpGet(Get_Token_Url);

CloseableHttpResponse response = httpClient.execute(httpGet);

String resp;

try {

HttpEntity entity = response.getEntity();

resp = EntityUtils.toString(entity, "utf-8");

EntityUtils.consume(entity);

} finally {

response.close();

}

LoggerFactory.getLogger(getClass()).info(" resp:{}", resp);

return resp;

}

/**

* 获取toAuth(String Get_Token_Url)返回结果中键值对中access_token键的值

*

* @param corpid 应用组织编号 corpsecret 应用秘钥

*/

public String getToken(String corpid, String corpsecret) throws IOException {

SendWeChatUtils sw = new SendWeChatUtils();

IacsUrlDataVo uData = new IacsUrlDataVo();

uData.setGet_Token_Url(corpid, corpsecret);

String resp = sw.toAuth(uData.getGet_Token_Url());

Map map = gson.fromJson(resp,

new TypeToken>() {

}.getType());

return map.get("access_token").toString();

}

/**

* @param touser 发送消息接收者 ,msgtype消息类型(文本/图片等),

* @param application_id 应用编号。

* @return String

* @Title:创建微信发送请求post数据

*/

public String createpostdata(String touser, String msgtype,

int application_id, String contentKey, String contentValue) {

IacsWeChatDataVo wcd = new IacsWeChatDataVo();

wcd.setTouser(touser);

wcd.setAgentid(application_id);

wcd.setMsgtype(msgtype);

Map content = new HashMap();

content.put(contentKey, contentValue + "\n--------\n" + df.format(new Date()));

wcd.setText(content);

return gson.toJson(wcd);

}

/**

* @param charset 消息编码 ,contentType 消息体内容类型,

* @param url 微信消息发送请求地址,data为post数据,token鉴权token

* @return String

* @Title 创建微信发送请求post实体

*/

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

}

LoggerFactory.getLogger(getClass()).info(

"call [{}], param:{}, resp:{}", url, data, resp);

return resp;

}

/**

* 处理微信接收人信息

*

* @return java.lang.String

* @Author shenlele

* @Date 2018/10/12 10:54

* @Param [receiver]

**/

public String toUser(String[] receiver) {

String[] arr = this.toString(receiver).split(",");

StringBuffer sb = new StringBuffer();

//将不含有@符号的邮箱地址取出

for (String str : arr) {

if (str.indexOf("@") == -1) {

sb.append(str + "|");

}

}

return sb.toString().substring(0, sb.length() - 1);

}

/**

* 数组转字符串

*

* @return java.lang.String

* @Author shenlele

* @Date 2018/10/12 11:27

* @Param [array]

**/

public String toString(String[] array) {

StringBuffer sb = new StringBuffer();

for (String str : array) {

sb.append(str + ",");

}

return sb.toString().substring(0, sb.length() - 1);

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值