package com.yeshi.fanli.util.push;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.Map;
|
|
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.HttpException;
|
import org.apache.commons.httpclient.methods.PostMethod;
|
import org.apache.commons.httpclient.methods.StringRequestEntity;
|
import org.yeshi.utils.HttpUtil;
|
|
import com.yeshi.fanli.exception.push.VIVOPushException;
|
import com.yeshi.fanli.util.StringUtil;
|
|
import net.sf.json.JSONObject;
|
|
/**
|
* VIVO的推送帮助
|
*
|
* @author Administrator
|
*
|
*/
|
public class VIVOPushUtil {
|
|
/**
|
* POST请求
|
*
|
* @param url
|
* @param body
|
* @param headers
|
* @return
|
*/
|
private static String post(String url, String body, Map<String, String> headers) {
|
HttpClient httpClient = new HttpClient();
|
PostMethod pm = new PostMethod(url);
|
try {
|
pm.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8"));
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
|
if (headers != null) {
|
Iterator<String> its = headers.keySet().iterator();
|
while (its.hasNext()) {
|
String key = its.next();
|
pm.setRequestHeader(key, headers.get(key));
|
}
|
}
|
|
try {
|
httpClient.executeMethod(pm);
|
return pm.getResponseBodyAsString();
|
} catch (HttpException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 获取授权码
|
*
|
* @param appId
|
* @param appKey
|
* @param appSecret
|
* @return
|
* @throws VIVOPushException
|
*/
|
public static String getAuthToken(String appId, String appKey, String appSecret) throws VIVOPushException {
|
JSONObject params = new JSONObject();
|
long timestamp = System.currentTimeMillis();
|
params.put("appId", appId);
|
params.put("appKey", appKey);
|
params.put("timestamp", timestamp);
|
params.put("sign", StringUtil.Md5(appId + appKey + timestamp + appSecret));
|
String result = post("https://host:port/message/auth", params.toString(), null);
|
JSONObject resultJson = JSONObject.fromObject(result);
|
if (resultJson.optInt("result") != 0)
|
throw new VIVOPushException(resultJson.optInt("result"), resultJson.optString("desc"));
|
return resultJson.optString("authToken");
|
}
|
|
|
|
|
}
|