处理了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请求工具整理的更多相关文章

  1. Java http请求工具类

    该工具类可以调用POST请求或者Get请求,参数以Map的方式传入,支持获获取返回值,返回值接收类型为String HttpRequestUtil.java package com.util; imp ...

  2. java HTTP请求工具

    package HttpRequestTest; import java.io.BufferedReader; import java.io.InputStream; import java.io.I ...

  3. Java 发送 Https 请求工具类 (兼容http)

    依赖 jsoup-1.11.3.jar <dependency> <groupId>org.jsoup</groupId> <artifactId>js ...

  4. Http请求工具类(Java原生Form+Json)

    package com.tzx.cc.common.constant.util; import java.io.IOException; import java.io.InputStream; imp ...

  5. java模板模式项目中使用--封装一个http请求工具类

    需要调用http接口的代码继承FundHttpTemplate类,重写getParamData方法,在getParamDate里写调用逻辑. 模板: package com.crb.ocms.fund ...

  6. java jdk原生的http请求工具类

    package com.base; import java.io.IOException; import java.io.InputStream; import java.io.InputStream ...

  7. Elasticsearch Java Rest Client API 整理总结 (二) —— SearchAPI

    目录 引言 Search APIs Search API Search Request 可选参数 使用 SearchSourceBuilder 构建查询条件 指定排序 高亮请求 聚合请求 建议请求 R ...

  8. Java 性能分析工具 , 第 3 部分: Java Mission Control

    引言 本文为 Java 性能分析工具系列文章第三篇,这里将介绍如何使用 Java 任务控制器 Java Mission Control 深入分析 Java 应用程序的性能,为程序开发人员在使用 Jav ...

  9. Java 性能分析工具 , 第 2 部分:Java 内置监控工具

    引言 本文为 Java 性能分析工具系列文章第二篇,第一篇:操作系统工具.在本文中将介绍如何使用 Java 内置监控工具更加深入的了解 Java 应用程序和 JVM 本身.在 JDK 中有许多内置的工 ...

随机推荐

  1. 记第一次正式线上笔试(Tencent——正式考-技术研发类-综合-2018实习生招聘)

    选择题做的跟傻逼一样,不多说了..大学只打了ACM还不是计算机科班出身的我,连好多名词都不认识..... 三道编程题很简单,下面给出三道题的大致题意以及题解. 1.给出n和m,满足(2m)可以整除n. ...

  2. JavaScript求取水仙花数

    一.什么是水仙花数 水仙花数也称为超完全数字不变数.自幂数.阿姆斯壮数.阿姆是特朗数. 水仙花数是指一个三位数,每个位数上数字的3次幂之和等于数字它本身. 水仙花数是自幂数的一种,三位的三次自幂数才叫 ...

  3. SQL 以逗号分隔查询;调用自定义函数

    select col from [dbo].[GetInPara]('101,102,103',',') USE [xxx] GO /****** Object: UserDefinedFunctio ...

  4. httpClient和RestTemplate的使用

    1.httpClient的使用 <dependency> <groupId>org.apache.httpcomponents</groupId> <arti ...

  5. MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main re

    出现这个问题是因为工程是应用win32,必须要有main函数,修改方式为: configuration properties中General->configuration Type->将a ...

  6. java大文件上传

    上次遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败. 一开始以为是session过期或者文件大小受系统限制,导致的错误.查看了系统的配置文件没有看到文件大小限制,web.xml中sees ...

  7. 牛客网 TaoTao要吃鸡 ( 0/1背包变形 )

    题意 : 题目链接 分析 :  如果没有 BUG (即 h == 0 的时候)就是一个普通的 0 / 1 背包 需要讨论一下 h != 0 的情况 此时有就相当于有物品是有特权的 而且背包装有特权的物 ...

  8. windows下开启远程连接Mysql

    使用“Ctrl + R”组合键快速打开cmd窗口,并输入“cmd”命令,打开cmd窗口.  使用“mysql -uroot -proot”命令可以连接到本地的mysql服务.  使用“use mysq ...

  9. DVWA--XSS(stored)

    XSS 0X01 1.简介 跨站脚本(cross site script)为了避免与样式css混淆,所以简称为XSS. XSS是一种经常出现在web应用中的计算机安全漏洞,也是web中最主流的攻击方式 ...

  10. 生成base64位图片验证码

    import org.springframework.util.Base64Utils; import javax.imageio.ImageIO; import java.awt.*; import ...