依赖的包
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
请求方法封装
package com.test.utils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
/**
* @Auther: Xu
* @Date: 2022/11/15 - 11 - 15 - 16:52
* @Description: com.test.utils
* @version: 1.0
*/
@Slf4j
public class HttpClientUtils {
public static String post(Object params,String url){
try{
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
// postMethod.addRequestHeader("accept", "*/*");
//设置Content-Type,此处根据实际情况确定
postMethod.addRequestHeader("Content-Type", "application/json");
//添加请求参数
RequestEntity entity = new StringRequestEntity(JSON.toJSONString(params),"application/json","UTF-8");
postMethod.setRequestEntity(entity);
//http状态码
int code = httpClient.executeMethod(postMethod);
InputStream in = postMethod.getResponseBodyAsStream();
//下面将stream转换为String
StringBuffer sb = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
char[] b = new char[4096];
for(int n; (n = isr.read(b)) != -1;) {
sb.append(new String(b, 0, n));
}
String returnStr = sb.toString();
httpClient = null;
postMethod = null;
b = null;
return returnStr;
}catch (Exception e){
e.printStackTrace();
log.error("请求异常{}",e.getMessage());
return null;
}
}
public static String get(Map<String,Object> params,String url){
String returnStr = null;
try{
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
//设置Content-Type,此处根据实际情况确定
getMethod.addRequestHeader("Content-Type", "application/json");
//添加请求参数
getMethod.setQueryString(getUrlParamsByMap(params));
int code = httpClient.executeMethod(getMethod);
InputStream in = getMethod.getResponseBodyAsStream();
//下面将stream转换为String
StringBuffer sb = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
char[] b = new char[4096];
for(int n; (n = isr.read(b)) != -1;) {
sb.append(new String(b, 0, n));
}
returnStr = sb.toString();
httpClient = null;
getMethod = null;
b = null;
}catch (Exception e){
e.printStackTrace();
log.error("请求异常{}",e.getMessage());
}
return returnStr;
}
//将map处理成?a=1&b=2的形式
public static String getUrlParamsByMap(Map<String, Object> map) {
if (map == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, Object> entry : map.entrySet()) {
sb.append(entry.getKey() + "=" + entry.getValue());
sb.append("&");
}
String s = "?"+sb.toString();
if (s.endsWith("&")) {
s = s.substring(0,s.length()-1);
}
System.out.println(s);
return s;
}
}
使用方式
Map<String,Object> m = new HashMap<>();
m.put("id","b");
m.put("name","e");
String s = HttpClientUtils.post(m,"http://*:10001/getdata");
Map<String,Object> m = new HashMap<>();
m.put("id","b");
m.put("name","e");
String s = HttpClientUtils.get(m,"http://*::10001/getdatb");