httpClient的使用步骤
- 创建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