微信开发 获取Token、主动给企业成员发送消息

10 篇文章 0 订阅
package com.synale.test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class TestMWX {
	public static void main(String[] args) {
		//getToken();
		getOpenids();
		//sendMSGToUser();
	}
	private static  String soapRequestOfSaml ="";
	
	public static String getToken() {
		try {
			String CorpID="wxccdd59a11177f3643";
			String Secret="jyEm4LBu4kV74veZPPqM1BQ27Ivau4sW2MCLcTJAVW20OrOAEfwmind8H1FyuqSA4";
			String url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+CorpID+"&corpsecret="+Secret;
			PostMethod postmethod = new PostMethod(url);
			
			HttpClient httpClient = new HttpClient();
			int statusCode = httpClient.executeMethod(postmethod);
			System.err.println("statuscode = " + statusCode);
			String soapResponseData = postmethod.getResponseBodyAsString();
			System.out.println(soapResponseData);
			
			JSONObject jsonObject =  JSONObject.fromObject(soapResponseData); 
			
			return jsonObject.getString("access_token");
			
		}catch(Exception e){e.printStackTrace();}
		
		return null;
	}
	
	public static void sendMSGToUser()
	{

		/**
		 * 给所有的人群发消息
		 * @param  touser @all 给所有的用户,toparty @all 所有的部门、agentid指定的应用ID
		 */
		
		soapRequestOfSaml=""
			+"{                                                "
			+"   \"touser\": \"@all\",      "
			+"   \"toparty\": \"@all\",       "
			+"   \"totag\": \"@all\",             "
			+"   \"msgtype\": \"text\",                        "
			+"   \"agentid\": \"0\",                           "
			+"   \"text\": {                                   "
			+"       \"content\": \"Holiday Request For Pony\" "
			+"   },                                            "
			+"   \"safe\":\"0\"                                "
			+"}                                                ";
		/**
		 * 给指定的人发送消息
		 * @param toparty 指定部门、touser指定的人、agentid应用ID
		 */
		
		soapRequestOfSaml=""
			+"{                                                "
			+"   \"touser\": \"chenYongTu\",      "
			+"   \"toparty\": \"5\",       "
			+"   \"totag\": \"\",             "
			+"   \"msgtype\": \"text\",                        "
			+"   \"agentid\": \"0\",                           "
			+"   \"text\": {                                   "
			+"       \"content\": \" Ready Go\" "
			+"   },                                            "
			+"   \"safe\":\"0\"                                "
			+"}                                                ";
		
		try {
			String url ="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";
			url =url.replace("ACCESS_TOKEN", "7hSuemdQLWiIL1B4NZ1KFGOZIeQMBsyDKP4b1pnA-UICWcmAkRN4tyln6-AD6NFmW");
		        
			PostMethod postmethod = new PostMethod(url);
			
			byte[] b = soapRequestOfSaml.getBytes("UTF-8");
			InputStream is = new ByteArrayInputStream(b, 0, b.length);
			RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/xop+xml; charset=UTF-8; type=\"text/xml\"");
			postmethod.setRequestEntity(re);
			HttpClient httpClient = new HttpClient();
			int statusCode = httpClient.executeMethod(postmethod);
			System.err.println("statuscode = " + statusCode);
			String soapResponseData = postmethod.getResponseBodyAsString();
			System.out.println(soapResponseData);
			
		}catch(Exception e){}
	}
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值