基于jdk11和基于apache-httpclient的http请求工具类

2023-12-22 03:28

本文主要是介绍基于jdk11和基于apache-httpclient的http请求工具类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.基于apache-httpclient

需要引入依赖
  <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.5</version></dependency>

工具类如下:

package com.bw.edgeagent.common.util;import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bw.edgeagent.uitl.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.shaded.okhttp3.OkHttpClient;
import org.testcontainers.shaded.okhttp3.Request;
import org.testcontainers.shaded.okhttp3.RequestBody;
import org.testcontainers.shaded.okhttp3.Response;import javax.net.ssl.SSLContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.*;/*** @Author MXF* @Description Htpp请求工具类* @Date 2023/12/21 10:59*/
public class IotHttpClientUtil {private static Logger log = LoggerFactory.getLogger(IotHttpClientUtilbak.class);private final static String DEFAULT_ENCODE = "UTF-8";/*** 服务端返回的cookie,有更新则覆盖*/private static String COOKIE_VALUE = "";/*** 默认 10s 超时*/private static final int TIME_OUT = 10 * 1000;private IotHttpClientUtilbak() {}/*** 忽略 ssl** @return*/private static SSLContext buildIgnoreContext() {SSLContext sslContext = null;try {sslContext = SSLContexts.custom().setProtocol("TLSv1.2").build();} catch (NoSuchAlgorithmException | KeyManagementException e) {log.error(e.getMessage(), e);}return sslContext;}private static CloseableHttpClient getClient(int timeOut) {RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeOut).setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();SSLContext sslContext = buildIgnoreContext();// 注册Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslContext)).build();PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);return HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig(requestConfig).build();}/*** POST application/json请求** @param url     请求地址* @param jsonStr 请求数据json字符串* @param headers 请求头* @param timeOut 超时时间* @return*/public static String sendPostJson(String url, String jsonStr, Map<String, String> headers, int timeOut) {HttpPost post = new HttpPost(url);if (headers != null && headers.size() > 0) {for (Map.Entry<String, String> entry : headers.entrySet()) {post.setHeader(entry.getKey(), entry.getValue());}}StringEntity entity = new StringEntity(jsonStr, DEFAULT_ENCODE);entity.setContentEncoding(DEFAULT_ENCODE);entity.setContentType("application/json;charset=" + DEFAULT_ENCODE);post.setEntity(entity);return execute(post, timeOut);}public static String sendDelete(String url, Map<String, String> headers, int timeOut) {HttpDelete httpDelete = new HttpDelete(url);if (headers != null && headers.size() > 0) {for (Map.Entry<String, String> entry : headers.entrySet()) {httpDelete.setHeader(entry.getKey(), entry.getValue());}}return execute(httpDelete, timeOut);}public static String sendDelete(String url, Map<String, String> headers) {return sendDelete(url, headers, TIME_OUT);}/*** POST application/json请求** @param url     请求地址* @param jsonStr 请求数据json字符串* @param headers 请求头* @return*/public static String sendPostJson(String url, String jsonStr, Map<String, String> headers) {return sendPostJson(url, jsonStr, headers, TIME_OUT);}/*** POST application/json请求** @param url     请求地址* @param jsonStr 请求数据json字符串* @param timeOut 超时时间* @return*/public static String sendPostJson(String url, String jsonStr, int timeOut) {return sendPostJson(url, jsonStr, new HashMap<>(0), timeOut);}/*** POST application/json请求** @param url     请求地址* @param jsonStr 请求数据json字符串* @return*/public static String sendPostJson(String url, String jsonStr) {return sendPostJson(url, jsonStr, TIME_OUT);}/*** POST application/json请求** @param url  请求地址* @param data 请求数据* @return*/public static String sendPostJson(String url, Object data) {if (data == null) {data = new HashMap(0);}return sendPostJson(url, JSONUtil.toJsonStr(data));}/*** POST application/json请求** @param url          请求地址* @param jsonStr      请求数据json字符串* @param responseType 返回值类型* @return* @author lizhenjiang* @date 2020/05/30*/public static <T> T postJsonForObject(String url, String jsonStr, Class<T> responseType) {String result = sendPostJson(url, jsonStr);if (StringUtils.isNotBlank(result)) {return JSONUtil.toBean(result, responseType);} else {return null;}}/*** POST application/json请求** @param url          请求地址* @param jsonStr      请求数据json字符串* @param responseType 返回值类型* @return* @author lizhenjiang* @date 2020/05/30*//* public static <T> T postJsonForObject(String url, String jsonStr, TypeReference<T> responseType) {String result = sendPostJson(url, jsonStr);if (StringUtils.isNotBlank(result)) {return JSONUtil.toBean(result, responseType);} else {return null;}}*//*** POST application/json请求** @param url          请求地址* @param data         请求数据* @param responseType 返回值类型* @return* @author lizhenjiang* @date 2020/05/30*/public static <T> T postJsonForObject(String url, Object data, Class<T> responseType) {if (data == null) {data = new HashMap(0);}return postJsonForObject(url, JSONUtil.toJsonStr(data), responseType);}/*** POST application/json请求** @param url          请求地址* @param data         请求数据* @param responseType 返回值类型* @return* @author lizhenjiang* @date 2020/05/30*/public static <T> T postJsonForObject(String url, Object data, T responseType) {if (data == null) {data = new HashMap(0);}return postJsonForObject(url, JSONUtil.toJsonStr(data), responseType);}/*** POST application/x-www-form-urlencoded 请求** @param url    请求地址* @param params 请求数据map* @return*/public static String sendPostForm(String url, Map<String, Object> params) {// 手动添加CookiesMap<String, String> hearParams = new HashMap<>();if (ObjectUtil.isNotEmpty(COOKIE_VALUE)) {// 添加Cookie到请求头hearParams.put("Cookie", COOKIE_VALUE);}return sendPostForm(url, params, hearParams);}/*** POST application/x-www-form-urlencoded 请求** @param url     请求地址* @param params  请求数据map* @param timeOut 超时时间* @return*/public static String sendPostForm(String url, Map<String, Object> params, int timeOut) {Map<String, String> headers = new HashMap<>(1);return sendPostForm(url, params, headers, timeOut);}/*** POST application/x-www-form-urlencoded 请求** @param url     请求地址* @param params  请求数据map* @param headers 请求头* @return*/public static String sendPostForm(String url, Map<String, Object> params, Map<String, String> headers) {return sendPostForm(url, params, headers, TIME_OUT);}/*** POST application/x-www-form-urlencoded 请求** @param url     请求地址* @param params  请求数据map* @param headers 请求头* @param timeOut 超时时间* @return*/public static String sendPostForm(String url, Map<String, Object> params, Map<String, String> headers, int timeOut) {UrlEncodedFormEntity reqEntity = createFormEntity(params);HttpPost httppost = new HttpPost(url);httppost.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");if (headers != null) {for (Map.Entry<String, String> entry : headers.entrySet()) {if (StrUtil.equalsAnyIgnoreCase(entry.getKey(), HttpHeaders.CONTENT_LENGTH, HttpHeaders.CONTENT_TYPE)) {continue;}httppost.addHeader(entry.getKey(), entry.getValue());}}httppost.setEntity(reqEntity);return execute(httppost, timeOut);}/*** POST application/x-www-form-urlencoded 请求** @param url          请求地址* @param params       请求数据map* @param responseType 返回值类型* @return* @author lizhenjiang* @date 2020/05/30*/
/*        public static <T> T postFormForObject(String url, Map<String, String> params, Class<T> responseType) {String result = sendPostForm(url, params);if (StringUtils.isNotBlank(result)) {return JSONUtil.toBean(result, responseType);} else {return null;}}*//*** POST application/x-www-form-urlencoded 请求** @param url          请求地址* @param params       请求数据map* @param responseType 返回值类型* @return* @author lizhenjiang* @date 2020/05/30*/public static <T> T postFormForObject(String url, Map<String, Object> params, Class<T> responseType) {String result = sendPostForm(url, params);if (StringUtils.isNotBlank(result)) {return JSONUtil.toBean(result, responseType);} else {return null;}}/*** GET 请求** @param url     请求地址* @param params  请求参数* @param timeOut 超时时间* @return*/public static String sendGet(String url, Map<String, Object> params, int timeOut) {return sendGet(url, params, new HashMap<>(1), timeOut);}/*** GET 请求** @param url    请求地址* @param params 请求参数* @return*/public static String sendGet(String url, Map<String, Object> params) {// 手动添加CookiesMap<String, String> hearParams = new HashMap<>();if (ObjectUtil.isNotEmpty(COOKIE_VALUE)) {// 添加Cookie到请求头hearParams.put("Cookie", COOKIE_VALUE);}return sendGet(url, params, hearParams);}/*** GET 请求** @param url     url* @param params  请求参数* @param headers 请求头* @param timeOut 超时时间* @return*/public static String sendGet(String url, Map<String, Object> params, Map<String, String> headers, int timeOut) {if (url == null) {return null;}try {URIBuilder uriBuilder = new URIBuilder(url);if (null != params) {uriBuilder.setParameters(getNameValuePairList(params));}URI uri = uriBuilder.build();String rawQueryString = uri.getRawQuery();//拼接urlif (StringUtils.isNotBlank(rawQueryString)) {// 防止原本url里面就有参数if (!url.contains("?")) {url = url + "?";}if (url.endsWith("?")) {url = url + rawQueryString;} else {url = url + "&" + rawQueryString;}}HttpGet httpGet = new HttpGet(url);if (headers != null) {Set<Map.Entry<String, String>> entrySet = headers.entrySet();for (Map.Entry<String, String> entry : entrySet) {httpGet.setHeader(entry.getKey(), entry.getValue());}}return execute(httpGet, timeOut);} catch (URISyntaxException e) {log.error(e.getMessage(), e);}return null;}/*** GET 请求** @param url     请求地址* @param params  请求参数* @param headers 请求头* @return*/public static String sendGet(String url, Map<String, Object> params, Map<String, String> headers) {return sendGet(url, params, headers, TIME_OUT);}/*** GET 请求** @param url          请求地址* @param params       请求参数* @param responseType 返回值类型* @return* @author lizhenjiang* @date 2020/05/30*/public static <T> T getForObject(String url, Map<String, Object> params, Class<T> responseType) {String result = sendGet(url, params);if (StringUtils.isNotBlank(result)) {return JSONUtil.toBean(result, responseType);} else {return null;}}private static List<NameValuePair> getNameValuePairList(Map<String, Object> params) {List<NameValuePair> list = new ArrayList<>();try {if (params != null && !params.isEmpty()) {for (String key : params.keySet()) {String value = params.get(key).toString();if (value != null) {list.add(new BasicNameValuePair(key, value));}}}} catch (Exception e) {log.error(e.getMessage(), e);}return list;}private static UrlEncodedFormEntity createFormEntity(Map<String, Object> pram) {try {List<NameValuePair> formParams = getNameValuePairList(pram);return new UrlEncodedFormEntity(formParams, DEFAULT_ENCODE);} catch (UnsupportedEncodingException e) {log.error(e.getMessage(), e);}return null;}private static String execute(HttpRequestBase requestBase, int timeOut) {StringBuilder sb = new StringBuilder();BufferedReader reader = null;CloseableHttpResponse response = null;CloseableHttpClient httpClient = getClient(timeOut);try {// 执行long start = System.currentTimeMillis();response = httpClient.execute(requestBase);//Header[] headers = response.getAllHeaders();//获取服务端返回的cookie并设置到全局变量,下一次发起请求使用,有更新则覆盖Header[] cookies = response.getHeaders("Set-Cookie");if (ObjectUtil.isNotEmpty(cookies)) {System.out.println(cookies);StringBuilder cookieValue = new StringBuilder();for (Header h : cookies) {cookieValue.append(h.getValue());}COOKIE_VALUE = cookieValue.toString();}log.info("请求id: {} , url: {} , 耗时: {} ", requestBase.getURI().toString(), (System.currentTimeMillis() - start));HttpEntity entity = response.getEntity();reader = new BufferedReader(new InputStreamReader(entity.getContent(), DEFAULT_ENCODE));String line = reader.readLine();while (line != null) {sb.append(line);line = reader.readLine();}EntityUtils.consume(entity);} catch (Exception e) {log.error("远程调用异常", e);} finally {try {if (reader != null) {reader.close();}if (response != null) {response.close();}httpClient.close();} catch (IOException e) {log.error("", e);}}return sb.toString();}/*** POST   multipart/form-data  请求** @param requestUrl* @param body* @return*/public static String sendPostWithFile(String requestUrl, RequestBody body) {try {OkHttpClient client = new OkHttpClient().newBuilder().build();Request request = new Request.Builder().url(requestUrl).method("POST", body).addHeader("Content-Type", "multipart/form-data").build();Response response = client.newCall(request).execute();if (response.body() == null) {log.info("短信发送httpClent获取数据为空");return null;}log.info("from-data:" + response.body().toString());return response.body().string();} catch (Exception e) {log.info("******短信发送httpClent  请求出错****" + e.getMessage());} finally {}return null;}
}

2.基于jdk11

工具类代码如下:

package com.bw.edgeagent.common.util;import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.List;
import java.util.Map;/*** @Author MXF* @Description Htpp请求工具类* @Date 2023/12/21 10:59*/
public class IotHttpUtil {private final static String DEFAULT_ENCODE = "UTF-8";/*** 服务端返回的cookie,有更新则覆盖*/private static String COOKIE_VALUE = "";/*** 默认 10s 超时*/private static final int TIME_OUT = 10 * 1000;private IotHttpUtil() {}/*** 发送GET请求** @param url* @return*/public static String get(String url) {return get(url, null);}/*** 发送GET请求** @param url* @param params* @return*/public static String get(String url, Map<String, String> params) {HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(URI.create(buildUrlWithParams(url, params)));if (ObjectUtil.isNotEmpty(COOKIE_VALUE)) {//手动添加Cookies,添加Cookie到请求头requestBuilder.header("Cookie", COOKIE_VALUE);}HttpRequest request = requestBuilder.header("Content-Type", "application/x-www-form-urlencoded").GET().build();return send(request);}/*** 给访问路径拼接参数** @param url* @param params* @return*/private static String buildUrlWithParams(String url, Map<String, String> params) {if (params == null || params.isEmpty()) {return url;}StringBuilder sb = new StringBuilder(url);// 若已有参数则和之前的参数合并sb.append(url.indexOf('?') == -1 ? '?' : '&');// 循环拼接参数params.forEach((key, value) -> {if (StrUtil.isNotBlank(value)) {try {value = URLEncoder.encode(value, Charset.defaultCharset());} catch (Exception e) {throw new RuntimeException(e);}sb.append(key).append('=').append(value).append('&');}});// 去掉最后的&return sb.substring(0, sb.length() - 1);}/*** 发送POST请求** @param url* @param data* @return*/public static String post(String url, String data) {return post(url, data, null);}/*** 发送POST请求** @param url* @param data* @param params* @return*/public static String post(String url, String data, Map<String, String> params) {HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(URI.create(buildUrlWithParams(url, params)));if (ObjectUtil.isNotEmpty(COOKIE_VALUE)) {//手动添加Cookies,添加Cookie到请求头requestBuilder.header("Cookie", COOKIE_VALUE);}HttpRequest request = requestBuilder.header("Content-Type", "application/x-www-form-urlencoded").POST(HttpRequest.BodyPublishers.ofString(data, Charset.forName(Charset.defaultCharset().toString()))).build();return send(request);}/*** 发送带本地证书的POST请求** @param url* @param data* @param certFile* @param certPwd* @return*/public static String postWithCertificate(String url, String data, File certFile, char[] certPwd) {HttpRequest request = HttpRequest.newBuilder(URI.create(url)).header("Content-Type", "application/x-www-form-urlencoded").POST(HttpRequest.BodyPublishers.ofString(data, Charset.forName(Charset.defaultCharset().toString()))).build();try {// 实例化SSL上下文SSLContext sslContext = SSLContext.getInstance("TLS");// 实例化密钥管理工厂KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());// 实例化密钥库KeyStore keyStore = KeyStore.getInstance("PKCS12");// 加载证书文件和密码keyStore.load(new FileInputStream(certFile), certPwd);// 初始化密钥管理工厂keyManagerFactory.init(keyStore, certPwd);// 初始化SSL上下文sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());// 构建HTTP客户端实例HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();return client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body).get();} catch (Exception e) {throw new RuntimeException(e);}}/*** 发送http请求** @param request* @return*/private static String send(HttpRequest request) {HttpClient client = HttpClient.newHttpClient();try {HttpResponse<String> res = client.send(request, HttpResponse.BodyHandlers.ofString());HttpHeaders headers = res.headers();List<String> cookies = headers.allValues("set-cookie");//获取服务端返回的cookie并设置到全局变量,下一次发起请求使用,有更新则覆盖if (ObjectUtil.isNotEmpty(cookies)) {System.out.println(cookies);StringBuilder cookieValue = new StringBuilder();for (String c : cookies) {cookieValue.append(c);}COOKIE_VALUE = cookieValue.toString();}return res.body();} catch (InterruptedException | IOException e) {throw new RuntimeException(e);}}//    public static void main(String[] args) {
//        Map<String, String> loginParamMap = new HashMap<>();
//        loginParamMap.put("username", "admin");
//        //密码admin 密码MD5加密32位
//        String password = DigestUtil.md5Hex("admin");
//        loginParamMap.put("password", password);
//        //String res = HttpUtil.sendGet(WVP_URL + WvpVideoUrlEnums.登录.getUrl(), HttpUtil.asUrlParams(loginParamMap));
//        String res = get("http://10.1.7.33:38080" + WvpVideoUrlEnums.登录.getUrl(), loginParamMap);
//        System.out.println("登录结果----->" + res);
//    }}

说明:写这两个工具类的起因是客户端发起请求登录服务端成功后,后续请求需要携带服务端返回的响应头cookie会话信息来鉴权,否则后续请求会报401

这篇关于基于jdk11和基于apache-httpclient的http请求工具类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/522429

相关文章

高效录音转文字:2024年四大工具精选!

在快节奏的工作生活中,能够快速将录音转换成文字是一项非常实用的能力。特别是在需要记录会议纪要、讲座内容或者是采访素材的时候,一款优秀的在线录音转文字工具能派上大用场。以下推荐几个好用的录音转文字工具! 365在线转文字 直达链接:https://www.pdf365.cn/ 365在线转文字是一款提供在线录音转文字服务的工具,它以其高效、便捷的特点受到用户的青睐。用户无需下载安装任何软件,只

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

【Linux 从基础到进阶】Ansible自动化运维工具使用

Ansible自动化运维工具使用 Ansible 是一款开源的自动化运维工具,采用无代理架构(agentless),基于 SSH 连接进行管理,具有简单易用、灵活强大、可扩展性高等特点。它广泛用于服务器管理、应用部署、配置管理等任务。本文将介绍 Ansible 的安装、基本使用方法及一些实际运维场景中的应用,旨在帮助运维人员快速上手并熟练运用 Ansible。 1. Ansible的核心概念

【Linux】应用层http协议

一、HTTP协议 1.1 简要介绍一下HTTP        我们在网络的应用层中可以自己定义协议,但是,已经有大佬定义了一些现成的,非常好用的应用层协议,供我们直接使用,HTTP(超文本传输协议)就是其中之一。        在互联网世界中,HTTP(超文本传输协议)是一个至关重要的协议,他定义了客户端(如浏览器)与服务器之间如何进行通信,以交换或者传输超文本(比如HTML文档)。

超强的截图工具:PixPin

你是否还在为寻找一款功能强大、操作简便的截图工具而烦恼?市面上那么多工具,常常让人无从选择。今天,想给大家安利一款神器——PixPin,一款真正解放双手的截图工具。 想象一下,你只需要按下快捷键就能轻松完成多种截图任务,还能快速编辑、标注甚至保存多种格式的图片。这款工具能满足这些需求吗? PixPin不仅支持全屏、窗口、区域截图等基础功能,它还可以进行延时截图,让你捕捉到每个关键画面。不仅如此

如何确定 Go 语言中 HTTP 连接池的最佳参数?

确定 Go 语言中 HTTP 连接池的最佳参数可以通过以下几种方式: 一、分析应用场景和需求 并发请求量: 确定应用程序在特定时间段内可能同时发起的 HTTP 请求数量。如果并发请求量很高,需要设置较大的连接池参数以满足需求。例如,对于一个高并发的 Web 服务,可能同时有数百个请求在处理,此时需要较大的连接池大小。可以通过压力测试工具模拟高并发场景,观察系统在不同并发请求下的性能表现,从而

Anaconda 中遇到CondaHTTPError: HTTP 404 NOT FOUND for url的问题及解决办法

最近在跑一个开源项目遇到了以下问题,查了很多资料都大(抄)同(来)小(抄)异(去)的,解决不了根本问题,费了很大的劲终于得以解决,记录如下: 1、问题及过程: (myenv) D:\Workspace\python\XXXXX>conda install python=3.6.13 Solving environment: done.....Proceed ([y]/n)? yDownloa

PR曲线——一个更敏感的性能评估工具

在不均衡数据集的情况下,精确率-召回率(Precision-Recall, PR)曲线是一种非常有用的工具,因为它提供了比传统的ROC曲线更准确的性能评估。以下是PR曲线在不均衡数据情况下的一些作用: 关注少数类:在不均衡数据集中,少数类的样本数量远少于多数类。PR曲线通过关注少数类(通常是正类)的性能来弥补这一点,因为它直接评估模型在识别正类方面的能力。 精确率与召回率的平衡:精确率(Pr

husky 工具配置代码检查工作流:提交代码至仓库前做代码检查

提示:这篇博客以我前两篇博客作为先修知识,请大家先去看看我前两篇博客 博客指路:前端 ESlint 代码规范及修复代码规范错误-CSDN博客前端 Vue3 项目开发—— ESLint & prettier 配置代码风格-CSDN博客 husky 工具配置代码检查工作流的作用 在工作中,我们经常需要将写好的代码提交至代码仓库 但是由于程序员疏忽而将不规范的代码提交至仓库,显然是不合理的 所

10个好用的AI写作工具【亲测免费】

1. 光速写作 传送入口:http://u3v.cn/6hXWYa AI打工神器,一键生成文章&ppt 2. 讯飞写作 传送入口:http://m6z.cn/5ODiSw 3. 讯飞绘文 传送入口:https://turbodesk.xfyun.cn/?channelid=gj3 4. AI排版助手 传送入口:http://m6z.cn/6ppnPn 5. Kim