1. 程式人生 > >微信公眾號客服消息提醒

微信公眾號客服消息提醒

end cati https rac pcl ant int Nid tst

微信客服消息提醒:
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");
  }

} 

微信公眾號客服消息提醒