前言
一个http工具类
代码
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.util.Map;
public class HttpUtils {
public interface ResponseCallback {
void onResponse(CloseableHttpResponse response) throws Exception;
void onError(int resultCode, String resultStr) throws Exception;
}
public static void headerPost(String urlStr, String data, ResponseCallback callback, Map<String, String> headerMap) throws Exception {
doPost(urlStr, data, callback, headerMap, 300000);
}
private static void doPost(String urlStr, String data, ResponseCallback callback, Map<String, String> headerMap, int timeOut) throws Exception {
HttpUriRequest request = null;
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try {
request = new HttpPost(urlStr);
if (headerMap != null) {
for(Map.Entry<String, String> entry : headerMap.entrySet()){
request.setHeader(entry.getKey(), entry.getValue());
}
}
if (null != data && !"".equals(data)) {
((HttpPost) request).setEntity(new ByteArrayEntity(data.getBytes("utf-8")));
}
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(timeOut).setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();
client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
response = client.execute(request);
int resultCode = response.getStatusLine().getStatusCode();
if (resultCode == HttpStatus.SC_OK) {
callback.onResponse(response);
} else {
throw new Exception(resultCode + ":system error");
}
} catch (Exception e) {
callback.onError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString());
} finally {
if (request != null && !request.isAborted()) {
request.abort();
request = null;
}
if (client != null) {
HttpClientUtils.closeQuietly(client);
client = null;
}
if (response != null) {
HttpClientUtils.closeQuietly(response);
response = null;
}
}
}
}
public class PostGo {
public static String postGo(String urlStr, String appid, String data) throws Exception {
final Map<String, String> resultMap = new HashMap<String, String>();
Map<String, String> headerMap = new HashMap<String, String>();
headerMap.put("Content-Type", "application/json");
if (appid != null) {
headerMap.put("appid", appid);
}
HttpUtils.ResponseCallback callback = new HttpUtils.ResponseCallback() {
@Override
public void onResponse(CloseableHttpResponse response) throws Exception {
try {
String resultStr = EntityUtils.toString(response.getEntity(), "utf-8");
resultMap.put("resultStr", resultStr);
} catch (Exception e) {
throw new Exception("onResponse error");
}
}
@Override
public void onError(int resultCode, String resultStr) throws Exception {
throw new Exception("onError error" + resultCode);
}
};
HttpUtils.headerPost(urlStr, data, callback, headerMap);
return resultMap.get("resultStr");
}
}