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){}
}
}