package com.tianjian.property.util;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JieshunPostUtil {
private static final String PRELOAD_API_URL="http://****.com";// 第三方公共接口地址
private static final String PRELOAD_API_USERNAME="username";
private static final String PRELOAD_API_PASSWORD="password";
private static final String PRELOAD_API_SPEED="200k";
private static final String PRELOAD_API_VALIDATION_TYPE="MD5";
private static final int PRELOAD_API_NEST_TRACK_LEVEL=0;
/**
* 调用API公共接口
* @param fileUrl为文件url
* @return
*/
@SuppressWarnings("deprecation")
public static String doPreLoadRequest(String fileUrl) {
HttpClient httpClient=null;
HttpPost method=null;
String result="";
if(null != fileUrl && fileUrl.trim().length() > 0) {
// fileUrl=UrlUtil.getPreLoadUrl(fileUrl);
fileUrl=fileUrl;
httpClient=new DefaultHttpClient();
method=new HttpPost(PRELOAD_API_URL);
}
String parameters=genPreLoadParams(fileUrl);
if(method != null && parameters != null && parameters.trim().length() > 0) {
try {
method.setEntity(new ByteArrayEntity(parameters.getBytes("utf-8")));// 设置编码
HttpResponse response=httpClient.execute(method);
int statusCode=response.getStatusLine().getStatusCode();
if(statusCode == HttpStatus.SC_OK) {
result=EntityUtils.toString(response.getEntity());
}
} catch(IOException e) {
e.printStackTrace();
return result;
} finally {
httpClient.getConnectionManager().shutdown();
}
}
return result;
}
/**
* 生成公共接口url参数串
* @param fileUrl
* @return
*/
public static String genPreLoadParams(String fileUrl) {
if(null == fileUrl || fileUrl.trim().length() == 0) {
return "";
}
JSONObject params=new JSONObject();
JSONObject paramObj=new JSONObject();
JSONArray paramArray=new JSONArray();
params.put("username", PRELOAD_API_USERNAME);
params.put("password", PRELOAD_API_PASSWORD);
params.put("speed", PRELOAD_API_SPEED);
params.put("validationType", PRELOAD_API_VALIDATION_TYPE);
params.put("nest_track_level", PRELOAD_API_NEST_TRACK_LEVEL);
// params.put("startTime", genPreLoadExecuteTime());
paramObj.put("id", UUID.randomUUID());
paramObj.put("url", fileUrl);
//paramObj.put("md5", new Md5().getMD5ofStr(fileUrl));
paramArray.add(paramObj);
params.put("tasks", paramArray);
return params.toString();
}
/*
* 生成预加载处理时间
* @return string
*/
public static String genPreLoadExecuteTime() {
String executeTime="";
Calendar calendar=Calendar.getInstance();
if(calendar.get(Calendar.HOUR_OF_DAY) >= 21 && calendar.get(Calendar.HOUR_OF_DAY) <= 22) {// 晚上21,22点按流量收费,预处理时间推到第二天凌晨0点
calendar.add(Calendar.DATE, +1);
executeTime=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()).concat(" 00:00:00");
} else {// 其它时间段则设为当前时间
executeTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
}
return executeTime;
}
}
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JieshunPostUtil {
private static final String PRELOAD_API_URL="http://****.com";// 第三方公共接口地址
private static final String PRELOAD_API_USERNAME="username";
private static final String PRELOAD_API_PASSWORD="password";
private static final String PRELOAD_API_SPEED="200k";
private static final String PRELOAD_API_VALIDATION_TYPE="MD5";
private static final int PRELOAD_API_NEST_TRACK_LEVEL=0;
/**
* 调用API公共接口
* @param fileUrl为文件url
* @return
*/
@SuppressWarnings("deprecation")
public static String doPreLoadRequest(String fileUrl) {
HttpClient httpClient=null;
HttpPost method=null;
String result="";
if(null != fileUrl && fileUrl.trim().length() > 0) {
// fileUrl=UrlUtil.getPreLoadUrl(fileUrl);
fileUrl=fileUrl;
httpClient=new DefaultHttpClient();
method=new HttpPost(PRELOAD_API_URL);
}
String parameters=genPreLoadParams(fileUrl);
if(method != null && parameters != null && parameters.trim().length() > 0) {
try {
method.setEntity(new ByteArrayEntity(parameters.getBytes("utf-8")));// 设置编码
HttpResponse response=httpClient.execute(method);
int statusCode=response.getStatusLine().getStatusCode();
if(statusCode == HttpStatus.SC_OK) {
result=EntityUtils.toString(response.getEntity());
}
} catch(IOException e) {
e.printStackTrace();
return result;
} finally {
httpClient.getConnectionManager().shutdown();
}
}
return result;
}
/**
* 生成公共接口url参数串
* @param fileUrl
* @return
*/
public static String genPreLoadParams(String fileUrl) {
if(null == fileUrl || fileUrl.trim().length() == 0) {
return "";
}
JSONObject params=new JSONObject();
JSONObject paramObj=new JSONObject();
JSONArray paramArray=new JSONArray();
params.put("username", PRELOAD_API_USERNAME);
params.put("password", PRELOAD_API_PASSWORD);
params.put("speed", PRELOAD_API_SPEED);
params.put("validationType", PRELOAD_API_VALIDATION_TYPE);
params.put("nest_track_level", PRELOAD_API_NEST_TRACK_LEVEL);
// params.put("startTime", genPreLoadExecuteTime());
paramObj.put("id", UUID.randomUUID());
paramObj.put("url", fileUrl);
//paramObj.put("md5", new Md5().getMD5ofStr(fileUrl));
paramArray.add(paramObj);
params.put("tasks", paramArray);
return params.toString();
}
/*
* 生成预加载处理时间
* @return string
*/
public static String genPreLoadExecuteTime() {
String executeTime="";
Calendar calendar=Calendar.getInstance();
if(calendar.get(Calendar.HOUR_OF_DAY) >= 21 && calendar.get(Calendar.HOUR_OF_DAY) <= 22) {// 晚上21,22点按流量收费,预处理时间推到第二天凌晨0点
calendar.add(Calendar.DATE, +1);
executeTime=new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()).concat(" 00:00:00");
} else {// 其它时间段则设为当前时间
executeTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
}
return executeTime;
}
}