当前位置: 移动技术网 > IT编程>开发语言>Java > java http请求工具整理

java http请求工具整理

2019年10月09日  | 移动技术网IT编程  | 我要评论

卖点商情,办公室短租,行尸走肉漫画版

 

  处理了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);
}

}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网