微信客服消息提醒:
public static void Test(String openid) throws Exception{
String token = getWeiXinAccessToken.getToken();
String strJson = "{\"touser\" :\""+openid+"\",";
strJson += "\"msgtype\":\"text\",";
strJson += "\"text\":{";
strJson += "\"content\":\"这里写你要提示的信息!\"";
strJson += "}}";
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?&body=0&access_token=" + token;
post(url, strJson);
}
public static void post(String url, String json){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
try{
StringEntity s = new StringEntity(json.toString(),"UTF-8");
//s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
System.out.println(EntityUtils.toString(entity, "utf-8"));
}
}catch (Exception e) {
throw new RuntimeException(e);
}
}
public class getWeiXinAccessToken {
public static String getToken() {
String getResponse="";
String appid= "公众号AppID";
String secret= "公众号secret";
try{
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
int execute = httpClient.executeMethod(getMethod);
System.out.println("execute:"+execute);
getResponse = getMethod.getResponseBodyAsString();
} catch (IOException e){
e.printStackTrace();
}
System.out.println(getResponse);
JSONObject json=JSONObject.fromObject(getResponse);
return json.getString("access_token");
}
}
原文地址:https://www.cnblogs.com/3b2414/p/9328351.html