import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.utils.HttpsGet;
public class WechatSendMsg {
public static final String WECHAT_MSG_CORP_ID = "wx77231802e9e4ea14";
public static final String WECHAT_MSG_CORP_SECRET = "3irDewN7Hf4XqYnvg4gXO_n5Tf4pxA7tDqsUMfP6EZZKQLOb3dmMVsabwZ6AVaD2";
public static final int WECHAT_MSG_AGINT = 3;
private static String getAccessToken(String corpId, String corpSecret){
try{
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpId+"&corpsecret="+corpSecret+"";
HttpsGet hGet=new HttpsGet(url);
JSONObject jo = JSON.parseObject(hGet.DoGet());
return jo.getString("access_token");
}catch(Exception e){
e.printStackTrace();
}
return "";
}
public static String sendText(String msg,String toUsers){
try{
String access_token= getAccessToken(WECHAT_MSG_CORP_ID,WECHAT_MSG_CORP_SECRET);
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+access_token;
JSONObject jo = new JSONObject();
jo.put("touser", toUsers);
jo.put("msgtype", "text");
jo.put("agentid", WECHAT_MSG_AGINT);//应用ID
/* "toparty": " PartyID1 | PartyID2 ", 部门id
"totag": " TagID1 | TagID2 ",标签id */
JSONObject jo_text = new JSONObject();
jo_text.put("content", msg);
jo.put("text", jo_text);
jo.put("safe", 0);
/*System.out.println(access_token);
System.out.println(JSON.toJSONString(jo));*/
JSONObject rJo = JSON.parseObject(sendPost(url,JSON.toJSONString(jo)));
System.out.println("微信接口返回值:"+rJo.getString("errcode"));
return rJo.getString("errcode");
}catch(Exception e){
e.printStackTrace();
}
return "1";
}
/**
* 发送Postq请求
* @param urlStr
* @param param
* @return
*/
private static String sendPost(String urlStr, String param) {
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try {
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
//设置参数
httpConn.setDoOutput(true); //需要输出
httpConn.setDoInput(true); //需要输入
httpConn.setRequestMethod("POST"); //设置POST方式连接
//设置请求属性
httpConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
//建立输入流,向指向的URL传入参数
DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
dos.write(param.getBytes("utf-8"));
dos.flush();
dos.close();
//获得响应状态
int resultCode = httpConn.getResponseCode();
if (HttpURLConnection.HTTP_OK == resultCode) {
String readLine;
BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
while ((readLine = responseReader.readLine()) != null) {
result.append(readLine).append("\n");
}
responseReader.close();
}
} catch (Exception e) {
result.append("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result.toString();
}
}
Java调用微信发送文本接口
最新推荐文章于 2024-06-22 23:21:49 发布