测开学习三:HttpClient框架

本文介绍了HttpClient的使用步骤,从创建默认请求池开始,详细讲解了GET和POST请求的发送,以及响应处理和连接释放。此外,还提到了工具类HttpclientUtils的封装方法,并提及了Windows环境下使用抓包工具进行网络请求监测。
摘要由CSDN通过智能技术生成

httpClient的使用步骤

  1. 创建http 默认请求池
    CloseableHttpClient httpclient = HttpClients.createDefault();
    2.创建某个请求post、get
    HttpGet get = new HttpGet(“http://www.baidu.com”);
    HttpPost post
    post.setEntity
    3.调用第一步中创建好的实例的execute
    CloseableHttpResponse response = httpclient.execute(httpGet);
    4.处理 response 返回 HttpEntity
    entity = reponse.getEntity();
    result= EntityUtils.toString(reponse.getEntity(), “utf-8”);
    5.释放连接。无论执行方法是否成功,都必须释放连接(EntityUtils.consume(entity)

封装工具类:HttpclientUtils

package com.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

public class StringToUtils {
    public static Map<String, Object> convert(String str, String regex) {
        Map<String, Object> resultMap= new LinkedHashMap<>();
        if (StringUtils.isNotBlank(str)) {
            String[] stringArray = str.split(regex);
                for(int i=0;i<stringArray.length;i++) {
                    resultMap.put(stringArray[i].split("=")[0], stringArray[i].split("=")[1]);
                }

        }
        return resultMap;
    }
}

package com.utils;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.cli
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值