java http请求工具整理
程序员文章站
2023-11-06 13:53:58
处理了http 的get和post的请求,分别支持同步处理,异步处理两种方式下见代码。 ......
处理了http 的get和post的请求,分别支持同步处理,异步处理两种方式下见代码。
@slf4j
public class httputils {
/**
* 同步请求http请求 不推荐
*
* @param url
* @return
*/
public static byte[] httpgetsync(string url) {
httpget httpget = new httpget(url);
try (closeablehttpclient httpclient = httpclients.createdefault()) {
closeablehttpresponse response = httpclient.execute(httpget);
if (httpstatus.sc_ok == response.getstatusline().getstatuscode()) {
httpentity entity = response.getentity();
return utils.inputstream2bytes(entity.getcontent());
} else {
log.error("neturls httpgetsync {} url {} ", response.getstatusline().getstatuscode(), url);
}
} catch (ioexception exception) {
log.error("reinitexchangeconfig request exception {}", exception);
}
return null;
}
/**
* 指定执行器的http请求 推荐
*
* @param url
* @param exec
* @param callback
*/
public static void httpget(string url, executor exec, netresponse callback) {
gethttpexec().execute(() -> {
byte[] bytes = httpgetsync(url);
exec.execute(() -> {
callback.response(bytes);
});
});
}
/**
* httppost 请求异步推荐
*
* @param url
* @param data
* @param contenttype
* @return
*/
public static byte[] httppostsync(string url, string data, contenttype contenttype) {
closeablehttpclient httpclient = httpclients.createdefault();
httppost httppost = new httppost(url);
httppost.setentity(new stringentity(data, contenttype));
try (closeablehttpresponse response = httpclient.execute(httppost)) {
if (httpstatus.sc_ok == response.getstatusline().getstatuscode()) {
httpentity entity = response.getentity();
return utils.inputstream2bytes(entity.getcontent());
} else {
log.warn("http post failure {}, {}, {}", url, data, response.getstatusline().getstatuscode());
}
} catch (ioexception exception) {
log.warn("http post exception", exception);
}
return null;
}
/**
* httppost 请求异步推荐
*
* @param url
* @param data
* @param contenttype
* @param exec
* @param callback
*/
public static void httppost(string url, string data, contenttype contenttype, executor exec, netresponse callback) {
gethttpexec().execute(() -> {
byte[] bytes = httppostsync(url, data, contenttype);
exec.execute(() -> {
callback.response(bytes);
});
});
}
/**
* 不指定执行器的http请求 不推荐
*
* @param url
* @param callback
*/
public static void httpgetasync(string url, netresponse callback) {
gethttpexec().execute(() -> {
byte[] bytes = httpgetsync(url);
callback.response(bytes);
});
}
/**
* 不指定执行器的http请求 不推荐
*
* @param url
* @param callback
*/
public static void httpgetasync(string url, string data, contenttype contenttype, netresponse callback) {
gethttpexec().execute(() -> {
byte[] bytes = httppostsync(url, data, contenttype);
callback.response(bytes);
});
}
private static executorservice httpexec;
private static executorservice gethttpexec() {
if (httpexec == null) {
synchronized (httputils.class) {
if (httpexec == null) {
httpexec = executors.newcachedthreadpool();
}
}
}
return httpexec;
}
public interface netresponse {
void response(byte[] response);
}
}