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

最新文章

  1. php中的正则函数主要有三个-正则匹配,正则替换
  2. 求树的重心(POJ1655)
  3. sql server返回插入数据表的id,和插入时间
  4. ionic+cordova+angularJs监听刷新
  5. [转]C,C++开源项目中的100个Bugs
  6. Java判断空字符串
  7. mono for android 学习记录
  8. nginx 日志分割(简单、全面)
  9. css颜色值设置方式有哪些?以及如何随机一个颜色?
  10. redis数据库-VUE创建项目
  11. windows cmd.exe 将程序 stdout 输出到文件中
  12. Process 模块的方法
  13. python操作oracle实战
  14. 当visual studio的数据库项目遇到SQL71501
  15. Python学习之路【第一篇】-Python简介和基础入门
  16. LeetCode - Most Frequent Subtree Sum
  17. Exploring the world of Android :: Part 2
  18. OSGI企业应用开发(二)Eclipse中搭建Felix运行环境
  19. protected: C++ access control works on per-class basis, not on per-object basis
  20. input的text输入框设置大一点

热门文章

  1. 【c++】一道关于继承和析构的笔试题
  2. C#调用Python(二)
  3. 23.二叉搜索树的后序遍历(python)
  4. js 反斜杠 处理
  5. VSCode支持jsx自动补全
  6. 洛谷 P3049 Landscaping ( 贪心 || DP)
  7. CTSC&APIO 2017游记
  8. event.stopPropagation()和event.preventDefault(),return false的区别
  9. sqli-lab(8)
  10. [CSP-S模拟测试]:光线追踪(线段树)