HttpClient工具类,粘贴可用

本文介绍了一个基于HttpClient的工具类,该工具类支持GET和POST请求,包括无参数和带参数的请求方式。通过该工具类可以轻松实现HTTP请求的发送,并获取响应的状态码、响应内容长度及响应内容。

该工具类使用到的jar包

//使用maven管理jar包
<dependency>
	  <groupId>org.apache.httpcomponents</groupId>
	  <artifactId>httpclient</artifactId>
	  <version>4.5.5</version>
</dependency>
<dependency>
	  <groupId>com.alibaba</groupId>
	  <artifactId>fastjson</artifactId>
	  <version>1.2.40</version>
</dependency>

该工具类中涉及到的方法

get无参

/**
 * get无参数请求 ,可验收
 *
 * @param url
 * @return resultMap ---> 状态码:statusLine,响应内容长度:contentLength,响应内容:content
 */
public static Map<String, Object> doGet(String url) {

    //返回参数
    Map<String, Object> resultMap = new HashMap<String, Object>();

    //获得一个http客户端
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    //创建一个get请求
    HttpGet httpGet = new HttpGet(url);

    //请求响应模型
    CloseableHttpResponse httpResponse = null;

    try {
        //客户端发送get请求,并获得具体响应
        httpResponse = httpClient.execute(httpGet);

        //从响应模型中获取实体
        HttpEntity httpEntity = httpResponse.getEntity();

        //从响应模型中获取响应状态码
        StatusLine statusLine = httpResponse.getStatusLine();

        resultMap.put("statusLine", statusLine);

        if (null != httpResponse) {
            //响应内容长度
            long contentLength = httpEntity.getContentLength();

            //响应内容
            String content = EntityUtils.toString(httpEntity);

            resultMap.put("contentLength", contentLength);
            resultMap.put("content", content);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            //资源释放
            if (null != httpResponse) {
                httpResponse.close();
            }
            if (null != httpClient) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return resultMap;
        }
    }

}

get有参

/**
 * get有参请求 ,可验收
 *
 * @param url    请求路径
 * @param params 请求参数,拼接完成 如:name=张三&age=20
 * @return resultMap ---> 状态码:statusLine,响应内容长度:contentLength,响应内容:content
 */
public static Map<String, Object> doGet(String url, String params) {

    //返回参数
    Map<String, Object> resultMap = new HashMap<String, Object>();

    //获得一个http客户端
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    //请求参数,编码转换,避免特殊字符串无法传递.当参数乱码时,可将该段代码放开
    /*try {
        params = URLEncoder.encode(params, "utf8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }*/


    //创建一个get请求
    HttpGet httpGet = new HttpGet(url + "?" + params);
    System.out.println(url + "?" + params);

    //请求响应模型
    CloseableHttpResponse httpResponse = null;

    //创建配置信息对象
    RequestConfig requestConfig = RequestConfig.custom()
            //设置连接超时时间
            .setConnectTimeout(5000)
            //设置请求超时时间
            .setConnectionRequestTimeout(5000)
            //设置读写超时时间
            .setSocketTimeout(5000)
            //设置是否允许重定向
            .setRedirectsEnabled(true)
            .build();

    //将配置信息修改入请求中
    httpGet.setConfig(requestConfig);

    try {

        //客户端发送get请求,并获得具体响应
        httpResponse = httpClient.execute(httpGet);

        //从响应模型中获取实体
        HttpEntity httpEntity = httpResponse.getEntity();

        //从响应模型中获取响应状态码
        StatusLine statusLine = httpResponse.getStatusLine();

        resultMap.put("statusLine", statusLine);

        if (null != httpResponse) {
            //响应内容长度
            long contentLength = httpEntity.getContentLength();

            //响应内容
            String content = EntityUtils.toString(httpEntity);

            resultMap.put("contentLength", contentLength);
            resultMap.put("content", content);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            //资源释放
            if (null != httpResponse) {
                httpResponse.close();
            }
            if (null != httpClient) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return resultMap;
        }
    }

}

post无参

/**
 * post无参请求 , 可验收
 * @param url
 * @return resultMap ---> 状态码:statusLine,响应内容长度:contentLength,响应内容:content
 */
public static Map<String, Object> doPost(String url) {

    //返回参数准备
    Map<String, Object> resultMap = new HashMap<String, Object>();

    //获得一个http客户端
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    //创建一个post请求
    HttpPost httpPost = new HttpPost(url);

    //创建一个可关闭的响应模型
    CloseableHttpResponse httpResponse = null;

    try {
        //http客户端发送post请求
        httpResponse = httpClient.execute(httpPost);

        //从响应模型中获取实体
        HttpEntity httpEntity = httpResponse.getEntity();

        //从响应模型中获取响应状态码
        StatusLine statusLine = httpResponse.getStatusLine();

        resultMap.put("statusLine", statusLine);

        if (null != httpResponse) {
            //响应内容长度
            long contentLength = httpEntity.getContentLength();

            //响应内容
            String content = EntityUtils.toString(httpEntity);

            resultMap.put("contentLength", contentLength);
            resultMap.put("content", content);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            //资源释放
            if (null != httpResponse) {
                httpResponse.close();
            }
            if (null != httpClient) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return resultMap;
        }
    }

}

post有参

/**
 * post有参请求 , 可验收
 * @param url
 * @param object 对象参数
 * @return resultMap ---> 状态码:statusLine,响应内容长度:contentLength,响应内容:content
 */
public static Map<String, Object> doPost(String url, Object object) {

    //请求参数准备
    Map<String,Object> resultMap = new HashMap<String,Object>();

    //创建http客户端
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    //创建一个post请求
    HttpPost httpPost = new HttpPost(url);

    //生成object的相应json字符串
    String jsonString = JSON.toJSONString(object);

    //对字符串进行相应的转码
    StringEntity stringEntity = new StringEntity(jsonString,"UTF-8");

    //将请求体放置post请求中
    httpPost.setEntity(stringEntity);

    //设置请求头,json类型
    httpPost.setHeader("Content-Type","application/json;charset=utf8");

    //创建一个可关闭的响应模型
    CloseableHttpResponse httpResponse = null;

    try {

        //http客户端发送post请求
        httpResponse = httpClient.execute(httpPost);

        //从响应模型中获取响应体
        HttpEntity httpEntity = httpResponse.getEntity();

        //从响应模型中获取响应状态码
        StatusLine statusLine = httpResponse.getStatusLine();

        resultMap.put("statusLine", statusLine);

        if (null != httpResponse) {
            //响应内容长度
            long contentLength = httpEntity.getContentLength();

            //响应内容
            String content = EntityUtils.toString(httpEntity);

            resultMap.put("contentLength", contentLength);
            resultMap.put("content", content);
        }

    } catch (IOException e) {

        e.printStackTrace();

    } finally {
        try {
            //资源释放
            if (null != httpResponse) {
                httpResponse.close();
            }
            if (null != httpClient) {
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return resultMap;
        }
    }

}

个人心得

1、整理的还不是很全面,只涉及到了post传参为对象的时候的方法
2、自己测试后,可以使用,希望自己以后的工作中可以用自己的代码
3、不断的积累,让自己遇到问题不再发愁
### CTFHUB 平台上的 SSRF 漏洞详情 在 CTFHUB 平台上,SSRF (Server-Side Request Forgery, 服务器端请求伪造) 是一种常见且重要的漏洞类别。该类漏洞允许攻击者通过构造特定的 URL 或参数来诱导服务器向指定目标发起 HTTP 请求。 #### 基本概念与影响范围 通常情况下,SSRF 攻击旨在使受害的应用程序作为代理去访问那些对外部网络不可见但对应用程序可见的服务资源。这些内部服务可能存储敏感数据或提供管理接口,一旦被恶意利用可能导致严重的后果[^3]。 #### 利用方式实例分析 对于某些 PHP 应用环境下的 SSRF 场景,在实际操作过程中发现了一种特殊的 payload 构造方法: ```plaintext http://challenge-80b40c7bb0f917cb.sandbox.ctfhub.com:10800/?url=http://notfound.ctfhub.com@127.0.0.1/flag.php ``` 上述 Payload 的特点在于它巧妙地运用了 `@` 符号将外部域名指向本地 IP (`127.0.0.1`) ,从而绕过了简单的黑名单过滤机制并成功触发了 SSRF 行为,最终实现了对 `/flag.php` 文件内容的非法获取[^4]。 #### 解决方案建议 针对此类问题的有效缓解措施可以从以下几个方面入手: - **输入验证加强**:严格校验用户提交的数据合法性,特别是涉及远程调用功能时要特别谨慎处理 URL 参数。 - **白名单策略实施**:仅限于信任列表内的主机名/IP 进行通信交互;同时考虑加入 DNS 缓存刷新逻辑防止缓存投毒风险。 - **协议限制应用**:避免支持过多不必要的协议(如 gopher:// 等),减少潜在威胁面。 - **防火墙配置优化**:合理设置云服务商提供的安全组规则以及 VPC 内网 ACL 控制,阻止来自公网对私有子网内资产的未授权访问尝试。 综上所述,为了有效防范 SSRF 类型的安全隐患,开发团队应当综合采取多种防护手段相结合的方式,并持续关注最新的攻防动态和技术趋势以确保系统的安全性得到保障[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值