微信企业号主动发送

来源:互联网 发布:java apache 编辑:程序博客网 时间:2024/05/16 12:43

微信主动发送的原理:首先通过微信企业号申请来的ID和密码获取accessToken。有了accessToken可以调用微信接口主动发送。

package web.report.controller;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
 
import javax.net.ssl.HttpsURLConnection;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.DefaultHttpParams;
 
import net.sf.json.JSONObject;
 


import serviceApi.report.SystemPropertyServiceFace;
import web.report.vo.SystemPropertyVO;


public class WXSendInfo {
public WXSendInfo(){

}
    public WXSendInfo(String CORPID,String CORPSECRET){
    this.CORPID = CORPID;
    this.CORPSECRET = CORPSECRET;
    }
    String MSGTYPE = "text";
    String CORPID;
    String CORPSECRET;
    String CREATE_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
    public String getAccessToken(){
    String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+CORPID+"&corpsecret="+CORPSECRET;
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(ACCESS_TOKEN_URL);
    post.releaseConnection();
    post.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
    NameValuePair[] param = {new NameValuePair("corpid",CORPID),new NameValuePair("corpsecret",CORPSECRET)};
    DefaultHttpParams.getDefaultParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
    post.setRequestBody(param);
    String result = "";
    try {
client.executeMethod(post);
result = new String(post.getResponseBodyAsString().getBytes("gbk"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    JSONObject jsonObject = JSONObject.fromObject(result);
    result = (String)jsonObject.getString("access_token");
    System.out.println(result);
    post.releaseConnection();
    return result;
    }
    public void sendWeChatMsgText(String touser,String topary,String totag,String content,String safe){
    URL url;
    String ACCESS_TOKEN = getAccessToken();
    String action = CREATE_SESSION_URL + ACCESS_TOKEN;
    StringBuffer sb = new StringBuffer();
    sb.append("{");
    sb.append("\"touser\":" + "\""+touser + "\",");
    sb.append("\"topary\":" + "\"" + topary + "\",");
    sb.append("\"totag\":" + "\"" + totag + "\",");
    sb.append("\"msgtype\":"+ "\"" + MSGTYPE + "\",");
    sb.append("\"text\":" + "{");
        sb.append("\"content\":" + "\"" + content + "\"");
        sb.append("}");
        sb.append(",\"safe\":" + "\"" + safe + "\",");
        sb.append("\"agentid\":" + "\"" + totag + "\",");
        sb.append("\"debug\":" + "\"" + "1" + "\"");
        sb.append("}");
        String json = sb.toString();
        try {
url = new URL(action);
HttpsURLConnection http = (HttpsURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
http.setDoInput(true);
http.setDoOutput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");
System.setProperty("sun.net.client.defaultReadTimeout", "30000");
http.connect();
OutputStream os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String result = new String(jsonBytes,"UTF-8");
System.out.println(result);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
    }
}