package com.bestpay.messagecenter.push.common.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.HttpStatus;
/**
* Http请求工具类
*
* @author linxing
* @version HttpUtil.java, v 0.1 2016/11/21 17:52 linxing Exp $$
*/
@Slf4j
public class HttpUtil {
private HttpUtil() {
throw new IllegalAccessError("Utility class");
}
/**
* 连接超时时间,单位:毫秒
*/
private static int CONNECTION_TIME_OUT = 5000;
/**
* 等待超时时间,单位:毫秒
*/
private static int READ_TIME_OUT = 10000;
/**
* 将HashMap parameters类型的参数封闭到URL中
*
* @param hm map参数
* @return 包含参数的url地址
*/
public static String encodeParam(Map<String, String> hm) {
if (hm == null) {
return "";
}
StringBuilder sb = new StringBuilder();
boolean first = true;
Iterator<String> it = hm.keySet().iterator();
while (it.hasNext()) {
if (first) {
first = false;
} else {
sb.append("&");
}
String key = it.next();
sb.append(key + "=" + hm.get(key));
}
return sb.toString();
}
/**
* 发起http post连接请求
* @param url 连接地址
* @param data 参数
* @param charset 编码
* @return 字符串
* @Exception 异常
*/
public static String httpPost(String url, String data, String charset) {
log.debug("httpPost请求 url:{}", url);
log.debug("httpPost请求 param:{}", data);
HttpClient client = new HttpClient();
// 设置连接超时
client.getHttpConnectionManager().getParams().setConnectionTimeout(CONNECTION_TIME_OUT);
client.getParams().setSoTimeout(READ_TIME_OUT);
//关闭100-continue 握手
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
StringBuilder stringBuilder = new StringBuilder();
PostMethod method = new PostMethod(url);
InputStreamReader reader = null;
try {
// contentType决定了请求的返回值是xml类型还是json类型
method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
method.setRequestEntity(new StringRequestEntity(data,
"application/x-www-form-urlencoded", charset));
int statusCode = client.executeMethod(method);
if (statusCode == HttpStatus.SC_OK) {
reader = new InputStreamReader(method.getResponseBodyAsStream());
BufferedReader br = new BufferedReader(reader);
String str = "";
while ((str = br.readLine()) != null) {
stringBuilder.append(str);
}
}else{
StringBuilder error = new StringBuilder();
reader = new InputStreamReader(method.getResponseBodyAsStream());
BufferedReader br = new BufferedReader(reader);
String str = "";
while ((str = br.readLine()) != null) {
error.append(str);
}
log.error("http url:{},code:{} 错误消息:{}",url, statusCode,error.toString());
}
} catch (Exception e) {
log.error("调用sp接口异常:{},url:{},data{}", Throwables.getStackTraceAsString(e),url,data);
} finally {
try {
if(null != reader){
reader.close();
}
} catch (IOException e) {
log.error("调用sp流关闭异常:{}", Throwables.getStackTraceAsString(e));
}
method.releaseConnection();
}
return stringBuilder.toString();
}
}
http util
最新推荐文章于 2024-06-22 23:14:28 发布