版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/s524061267/article/details/76004563
本文旨在整理个人工作总结,仅供参考
- HttpClientUtil工具类源码:
import java.io.IOException;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
* @ClassName: HttpClientUtil
* @Description: HTTP-POST请求工具类
* @author sly.shuai
* @date2017年7月6日 上午10:29:20
* @version V1.0
*/
public class HttpClientUtil
{
/**
* @Title: doHttpRequest
* @Description: 发送http请求
* @param apiURL
* @param sendParam
* @return String
* @throws
* @变更记录 2017年7月18日 下午2:48:01 sly.shuai 创建
*/
public static String doHttpRequest(String apiURL, String sendParam) {
DefaultHttpClient httpClient = new DefaultHttpClient();// httpClient
HttpPost post = new HttpPost(apiURL); // POST请求
StringEntity entity = null;
HttpResponse response = null;
String responseContent = null; // 响应内容
try {
entity = new StringEntity(sendParam, "UTF-8"); // 防止中文乱码
entity.setContentEncoding("UTF-8");
post.setHeader("Content-Type", "application/json"); // 设置请求头
post.setEntity(entity);
// 从响应值中取得token
response = httpClient.execute(post);
// 返回状态码为200时处理结果
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();
responseContent = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return responseContent;
}
/**
* @Title: doHttpsRequest
* @Description: 发送https请求
* @param apiURL
* @param sendParam
* @return String
* @throws
* @变更记录 2017年7月18日 下午2:47:36 sly.shuai 创建
*/
public static String doHttpsRequest(String apiURL, String sendParam) {
HttpClient httpClient = null;// httpClient
HttpPost post = new HttpPost(apiURL); // POST请求
StringEntity entity = null;
HttpResponse response = null;
String responseContent = null; // 响应内容
try {
httpClient = new SSLClient();
entity = new StringEntity(sendParam, "UTF-8"); // 防止中文乱码
entity.setContentEncoding("UTF-8");
post.setHeader("Content-Type", "application/json"); // 设置请求头
post.setEntity(entity);
// 从响应值中取得token
response = httpClient.execute(post);
// 返回状态码为200时处理结果
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();
responseContent = EntityUtils.toString(httpEntity, "UTF-8");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return responseContent;
}
}
上面所引用到的SSLClient类源码:
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* @ClassName: SSLClient
* @Description:SSLClient
* @author sly.shuai
* @date2017年7月18日 上午11:37:02
* @version V1.0
*/
public class SSLClient extends DefaultHttpClient
{
public SSLClient() throws Exception {
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
// 允许所有证书
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
- 数据划分源码:
import java.util.List;
import java.util.Map;
/**
* @ClassName: DataPartition
* @Description:
* @author sly.shuai
* @date2017年7月24日 上午11:12:43
* @version V1.0
*/
@SuppressWarnings("rawtypes")
public class DataPartitionUtil
{
/**
* @Title: partitioningData
* @Description: 划分数据
* @param list
* void
* @throws
* @变更记录 2017年7月24日 上午11:35:37 sly.shuai 创建
*/
public static void partitioningData(List<Map> list) {
if (list != null && list.size() > 0) {
int total = list.size();// 总数
int avg = 10;// 每10条发送一次
int number = total / avg;// 发送次数
int remainder = total % avg;// 余数
int index = 0;// 索引
// 如果余数大于0则增加一次发送次数
if (remainder > 0) {
number = number + 1;
}
if (number > 0) {
for (int i = 0; i < number; i++) {
if (index + avg > total) {
List<Map> sonList = list.subList(avg * i, total);
// 这里处理最后剩下的数量不超过10的数据
} else {
List<Map> sonList = list.subList(avg * i, avg * i + avg);
// 这里处理划分的每10条数据
index += avg;
}
}
}
}
}
}
- 计算时间之差源码:
import java.util.Date;
/**
* @ClassName: DateDiffer
* @Description:
* @author sly.shuai
* @date2017年7月24日 上午11:38:51
* @version V1.0
*/
public class DateDiffer
{
/**
* @Title: getDistanceTimes
* @Description: 计算时间之差
* @param now
* @param old
* @return long[]
* @throws
* @变更记录 2017年7月7日 上午11:38:16 sly.shuai 创建
*/
public static long[] getDistanceTimes(Date now, Date old) {
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
long nh = 1000 * 60 * 60;// 一小时的毫秒数
long nm = 1000 * 60;// 一分钟的毫秒数
long ns = 1000;// 一秒钟的毫秒数
long diff;
long day = 0l;
long hour = 0l;
long min = 0l;
long sec = 0l;
try {
// 获得两个时间的毫秒时间差异
diff = now.getTime() - old.getTime();
day = diff / nd;// 计算差多少天
hour = diff % nd / nh;// 计算差多少小时
min = diff % nd % nh / nm;// 计算差多少分钟
sec = diff % nd % nh % nm / ns;// 计算差多少秒
} catch (Exception e) {
e.printStackTrace();
}
long[] times = { day, hour, min, sec };
return times;
}
}