日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java http请求插件_java http请求工具整理

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java http请求插件_java http请求工具整理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

處理了http 的get和post的請求,分別支持同步處理,異步處理兩種方式下見代碼。

@Slf4jpublic 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请求工具整理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。