企业微信api
企业号开发者接口文档
本文主要说明如何调用简单的接口发送消息到已注册的接口文档;
网址:>https://work.weixin.qq.com/?from=qyh_redirect
收集关键信息:CorpID,AgentId,Secret(在开发中用到参与加密或者秘钥等)
原理:根据关键信息,发送GET请求给微信企业服务器,获取秘钥token,在给企业号发送信息,根据各种发送给企业号的格式;具体细节参考官方文档;
package com.wxwork1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
public class TestAccessToken2 {
/**
* 程序入口:调用各种方法
*/
public static void main(String[] args) {
//发送 GET 请求
String corpid=" ";
String corpsecret =" " ;
String access_token=GetForAT(corpid,corpsecret);
System.out.println("access_token:"+access_token);
//获取accestoken后,发送post请求,目前发送txt格式,map为发送的参数
/*Map mapParam = new HashMap<>();
mapParam.put("touser", "1");
mapParam.put("toparty", "1");
mapParam.put("totag", "");
mapParam.put("msgtype", "text");
mapParam.put("agentid", "1000002");
mapParam.put("text", "{\"content\":\"0000000000\"}");
mapParam.put("safe", "0");
mapParam.put("text", "{content:你的快递已到,请携带工卡前往邮件中心领取}");*/
JSONObject jsparam = new JSONObject();
jsparam.put("content", "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看邮件中心视频实况,聪明避开排队。");
JSONObject jsparam2 = new JSONObject();
jsparam2.put("touser", "@all");
jsparam2.put("msgtype", "text");
jsparam2.put("agentid", 1000002);
jsparam2.put("text", jsparam);
jsparam2.put("safe", "0");
String sendruslt= sendData(access_token,jsparam2);
System.out.println("post请求返回结果:"+sendruslt);
}
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String GetForAT(String corpid,String corpsecret) {
String result = null;
BufferedReader in = null;
Map maps = new HashMap<>();
try {
String urlNameString = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid
+ "&corpsecret=" + corpsecret;
URL realUrl = new URL(urlNameString);
System.out.println(realUrl);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("Content-Type", "appliction/json;charset=UTF-8");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//http请求属性
System.out.println("http请求属性:"+connection.getRequestProperty("Content-Type"));
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println("http响应头文件:"+key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result=line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
String[] ss = result.substring(1, result.length()-1).split(",");
List ls = Arrays.asList(ss);
System.out.println(Arrays.toString(ss));
for(String temp:ls){
maps.put(temp.split(":")[0].replaceAll("\"",""), temp.split(":")[1].replaceAll("\"",""));
}
result=maps.get("access_token");
return result;
}
/**
* 接收微信服务器返回参数:access_token
* 发送带有参数access_token的信息
*/
public static String sendData(String access_token,JSONObject jsparam){
String url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+access_token;
PrintWriter out = null;
BufferedReader in = null;
String result = "";
System.out.println("post请求josn串:"+jsparam.toString());
try {
URL realUrl = new URL(url);
System.out.println("post请求地址:"+realUrl);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Type", "appliction/json;charset=UTF-8");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(jsparam);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
//System.out.println(result.toString());
return result;
}
}