java http 工具_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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值