java http请求工具整理
处理了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);
} }
java http请求工具整理的更多相关文章
- Java http请求工具类
该工具类可以调用POST请求或者Get请求,参数以Map的方式传入,支持获获取返回值,返回值接收类型为String HttpRequestUtil.java package com.util; imp ...
- java HTTP请求工具
package HttpRequestTest; import java.io.BufferedReader; import java.io.InputStream; import java.io.I ...
- Java 发送 Https 请求工具类 (兼容http)
依赖 jsoup-1.11.3.jar <dependency> <groupId>org.jsoup</groupId> <artifactId>js ...
- Http请求工具类(Java原生Form+Json)
package com.tzx.cc.common.constant.util; import java.io.IOException; import java.io.InputStream; imp ...
- java模板模式项目中使用--封装一个http请求工具类
需要调用http接口的代码继承FundHttpTemplate类,重写getParamData方法,在getParamDate里写调用逻辑. 模板: package com.crb.ocms.fund ...
- java jdk原生的http请求工具类
package com.base; import java.io.IOException; import java.io.InputStream; import java.io.InputStream ...
- Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI
目录 引言 Search APIs Search API Search Request 可选参数 使用 SearchSourceBuilder 构建查询条件 指定排序 高亮请求 聚合请求 建议请求 R ...
- Java 性能分析工具 , 第 3 部分: Java Mission Control
引言 本文为 Java 性能分析工具系列文章第三篇,这里将介绍如何使用 Java 任务控制器 Java Mission Control 深入分析 Java 应用程序的性能,为程序开发人员在使用 Jav ...
- Java 性能分析工具 , 第 2 部分:Java 内置监控工具
引言 本文为 Java 性能分析工具系列文章第二篇,第一篇:操作系统工具.在本文中将介绍如何使用 Java 内置监控工具更加深入的了解 Java 应用程序和 JVM 本身.在 JDK 中有许多内置的工 ...
随机推荐
- 在vue组件中使用vuex的state状态对象的5种方式
下面是store文件夹下的state.js和index.js内容 //state.js const state = { headerBgOpacity:0, loginStatus:0, count: ...
- 【NOIP2016提高A组模拟9.24】总结
第一题纯模拟,结果那个出题人脑子似乎进水了,空间限制开了1G!!! 导致我捉摸了半天为什么空间要开那么大,最后只能得出上面的结论. 第二题是个矩阵快速幂,比赛上我没把递推式求出来,但是根据各种乱搞,得 ...
- elasticsearch基本Restful操作
1.添加数据curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/emplo ...
- C#任务调度——LimitedConcurrencyLevelTaskScheduler
这是参考大佬分享的代码写的有问题请提出指正,谢谢. using Serilog; using System; using System.Collections.Generic; using Syste ...
- SpringBoot的项目构建
手动构建SpringBoot项目 一.手动构建一个Maven,选择...webapp或者选择快速骨架生成,然后命名并生成项目: 二. 在pom.xml中,进行三处配置: 设置父模块,子模块就可以继承 ...
- MySQL 运维管理平台
github: https://github.com/XiaohaoYu/mysql_platform
- Linux基础教程 linux下cat 命令使用详解
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...
- CF191C Fools and Roads - 树剖解法
Codeforces Round #121 (Div. 1) C. Fools and Roads time limit per test :2 seconds memory limit per te ...
- Linux安装vsftpd及配置详解
1 安装vsftpd组件 安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件.[root@bogon ~]# yum -y install vsftpd 2.FT ...
- pyCharm报错"your evaluation license has expired, pycharm will now exit"解决方法(实测)
一.修改C:\Windows\System32\drivers\etc 目录下的hosts文件 1.打开hosts文件,路径是 c:\windows\system32\drivers\etc\host ...