常用工具类---SQL工具,HTTP工具

本文涉及的产品
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
注册配置 MSE Nacos/ZooKeeper,182元/月
简介: SQL工具,HTTP工具,两个实用小工具~~~

是否包含sql关键字

public static boolean sqlValidate(String str) {
    if (null == str || "".equals(str)) {
        return false;
    }
    str = str.toLowerCase();// 统一转为小写
    String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|*|%|chr|mid|master|truncate|"
        + "char|declare|sitename|net user|xp_cmdshell|;|or|-|+|,|like'|and|exec|execute|insert|create|drop|"
        + "table|from|grant|use|group_concat|column_name|"
        + "information_schema.columns|table_schema|union|where|select|delete|update|order|by|count|*|"
        + "chr|mid|master|truncate|char|declare|or|;|-|--|+|,|like|//|/|%|#";// 过滤掉的sql关键字,可以手动添加
    String[] badStrs = badStr.split("\\|");
    for (int i = 0; i < badStrs.length; i++) {
        if (str.indexOf(badStrs[i]) >= 0) {
            return true;
        }
    }
    return false;
}

HTTP-GET带header请求

// authorization为授权验证,若无授权验证可删除
private static JSONObject sendGet(String url, String authorization) throws IOException {
    CloseableHttpClient cHttpClient = HttpClients.createDefault();
    HttpGet get = new HttpGet(url);
    get.addHeader("Content-Type", "application/json");
    get.addHeader("Authorization", authorization);
    CloseableHttpResponse response = cHttpClient.execute(get);
    logger.info("Http Comunication end ! code --> " + response.getStatusLine().getStatusCode());
    HttpEntity entity = response.getEntity();
    String responseContent = EntityUtils.toString(entity, "UTF-8");
    logger.info("URL=" + url + ",response=" + responseContent);
    response.close();
    cHttpClient.close();
    return JSONObject.parseObject(responseContent);
}

HTTP-POST带header请求

private static JSONObject sendPost(String businessUrl, JSONObject sendMsgBody, String accessToken) throws IOException {
    CloseableHttpClient cHttpClient = HttpClients.createDefault();
    HttpPost post = new HttpPost(businessUrl);
    post.addHeader("Content-Type", "application/json");
    post.addHeader("Authorization", "Bearer " + accessToken);
    post.setEntity(new StringEntity(sendMsgBody.toString(), "UTF-8"));
    CloseableHttpResponse response = cHttpClient.execute(post);
    logger.info("Http Comunication end ! code --> " + response.getStatusLine().getStatusCode());
    HttpEntity entity = response.getEntity();
    String responseContent = EntityUtils.toString(entity, "UTF-8");
    logger.info("URL=" + businessUrl + ",response=" + responseContent);
    response.close();
    cHttpClient.close();
    return JSONObject.parseObject(responseContent);
}

HTTPS-POST带header请求

 //设置链接超时和请求超时等参数,否则会长期停止或者崩溃
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();


public static String sendHttpsPost(String url, JSONObject params) {
    String responseContent = null;
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse httpResponse = null;
    try {
        HttpPost httpPost = new HttpPost(url);
        // header
        httpPost.addHeader("AppKey", SystemConstants.APP_KEY);
        httpPost.addHeader("Secret", SystemConstants.SECRET);
        // body
        httpPost.setEntity(new StringEntity(params.toString(), "UTF-8"));
        httpClient = HttpClients.custom().setSSLSocketFactory(createSslConnSocketFactory()).setDefaultRequestConfig(requestConfig).build();
        httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
            responseContent = EntityUtils.toString(httpEntity, "UTF-8");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if(null != httpResponse) {
                httpResponse.close();
            }
            if (null != httpClient) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return responseContent;
}

 /**
     * 创建SSL安全连接
     * @return SSLConnectionSocketFactory
     */
private static SSLConnectionSocketFactory createSslConnSocketFactory() {
    SSLConnectionSocketFactory sslsf = null;
    try {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
        sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }

            @Override
            public void verify(String host, SSLSocket ssl) {
            }

            @Override
            public void verify(String host, X509Certificate cert) {
            }

            @Override
            public void verify(String host, String[] cns, String[] subjectAlts) {
            }
        });
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }
    return sslsf;
}


相关文章
|
6月前
|
Java
|
3月前
|
Java 测试技术 容器
Jmeter工具使用:HTTP接口性能测试实战
希望这篇文章能够帮助你初步理解如何使用JMeter进行HTTP接口性能测试,有兴趣的话,你可以研究更多关于JMeter的内容。记住,只有理解并掌握了这些工具,你才能充分利用它们发挥其应有的价值。+
744 23
|
4月前
|
SQL 关系型数据库 MySQL
SQL在线美化工具
SQL 在线美化工具是一款智能代码格式化工具,专为开发者、数据分析师及数据库管理员设计。支持自动缩进、语法高亮、关键字优化(大写/小写)及语法错误提示,兼容MySQL、PostgreSQL等多种SQL方言,可快速将杂乱SQL语句转换为专业易读的格式,提升代码维护效率和团队协作体验。
377 19
|
8月前
|
SQL 大数据 数据处理
Flink SQL 详解:流批一体处理的强大工具
Flink SQL 是为应对传统数据处理框架中流批分离的问题而诞生的,它融合了SQL的简洁性和Flink的强大流批处理能力,降低了大数据处理门槛。其核心工作原理包括生成逻辑执行计划、查询优化和构建算子树,确保高效执行。Flink SQL 支持过滤、投影、聚合、连接和窗口等常用算子,实现了流批一体处理,极大提高了开发效率和代码复用性。通过统一的API和语法,Flink SQL 能够灵活应对实时和离线数据分析场景,为企业提供强大的数据处理能力。
1643 27
|
6月前
|
SQL Java
|
9月前
|
JSON 数据格式
.net HTTP请求类封装
`HttpRequestHelper` 是一个用于简化 HTTP 请求的辅助类,支持发送 GET 和 POST 请求。它使用 `HttpClient` 发起请求,并通过 `Newtonsoft.Json` 处理 JSON 数据。示例展示了如何使用该类发送请求并处理响应。注意事项包括:简单的错误处理、需安装 `Newtonsoft.Json` 依赖,以及建议重用 `HttpClient` 实例以优化性能。
238 2
|
10月前
|
SQL 存储 BI
gbase 8a 数据库 SQL合并类优化——不同数据统计周期合并为一条SQL语句
gbase 8a 数据库 SQL合并类优化——不同数据统计周期合并为一条SQL语句
|
11月前
|
SQL 数据可视化 安全
微软SQL Server可视化工具与技巧
微软SQL Server不仅提供了强大的数据库管理功能,还集成了多种可视化工具,帮助用户更直观地理解和管理数据
|
11月前
|
SQL 数据可视化 关系型数据库
【数据库工具】DBeaver:一款免费的通用数据库工具和 SQL 客户端
【数据库工具】DBeaver:一款免费的通用数据库工具和 SQL 客户端
1054 1