static String appid = "A6980295688164";
static String appkey = "F7539113-CE9E-7919-87DE-C0B9898FCBC4";
public static void main(String[] args) throws IOException {
try {
HttpClient client = new HttpClient();
// post请求
PostMethod post = new UTF8PostMethod("https://p.apicloud.com/api/push/message");
// 提交参数
NameValuePair title = new NameValuePair("title","推送没"); // 消息标题
NameValuePair content = new NameValuePair("content","推送了哇"); // 消息内容
NameValuePair type = new NameValuePair("type","2"); // 消息类型,1:消息 2:通知
NameValuePair platform = new NameValuePair("platform","0"); // 0:全部平台,1:ios, 2:android
// 推送组,推送用户(没有可不写)
//NameValuePair groupName = new NameValuePair("groupName","组名"); // 推送组名,多个组用英文逗号隔开.默认:全部组
//NameValuePair userIds = new NameValuePair("userIds","id名称"); // 推送用户id, 多个用户用英文逗号分隔
post.setRequestBody(new NameValuePair[]{title, content, type, platform/*, groupName, userIds*/});
HttpMethod method = post;
// 生成规则
String key = testInvokeScriptMethod();
// 设置请求头部信息
method.setRequestHeader("X-APICloud-AppId", appid);
method.setRequestHeader("X-APICloud-AppKey", key);
// 执行方法
client.executeMethod(method);
// 打印服务器返回的状态
System.out.println(method.getStatusLine());
// 打印结果页面
String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
// 打印返回的信息
System.out.println(response);
// 释放连接
method.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Java中调用脚本语言的方法,通过JDK平台给script的方法中的形参赋值
*
* @param engine ScriptEngine实例
* @return String
* */
private static String testInvokeScriptMethod() throws Exception {
// 获取时间戳
long now = new Date().getTime();
String key = appid + "UZ" + appkey + "UZ" + now;
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByName("js");
// sha1.js 路径 (可根据自己的需求改为项目存放的相对路径)
String sha1 = "f:/sha1.js";
// 调用js文件
FileReader fr = new FileReader(sha1);
engine.eval(fr); // 指定脚本
Invocable inv = (Invocable) engine;
// 调用js函数方法(SHA1)
String res = (String) inv.invokeFunction ("SHA1", key);
return res + "." + now;
}
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
return "UTF-8";
}
}