java http请求工具整理
2019-10-09 09:32:56来源:博客园 阅读 ()
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);
}
}
原文链接:https://www.cnblogs.com/hanchen007/p/11641606.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash