http发送get/post请求,普通传参
http 请求工具类,需引用合适的jar
import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; /** * description: http 请求工具类 */ public class HttpClientUtil {
public static final String reqSystemID = "aweb"; public static final String reqSystemName = "aweb端";
/** * description: 发送http get 请求 * @param url url * @return java.lang.String */ public static String doHttpGet(String url) { //创建HttpClient对象 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //创建HttpGet对象 HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response = null; try { //执行GET请求 response = httpClient.execute(httpGet); // System.out.println(response.getStatusLine()); //获取响应实体 HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //释放资源 try { response.close(); httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * description: 发送http post 请求 * @param url * @param json josn参数 * @return java.lang.String */ public static String doHttpPost(String url, String json) { //创建HttpClient对象 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); //创建HttpPost对象 HttpPost post = new HttpPost(url); //设置POST请求传递参数 try { post.setHeader(HTTP.CONTENT_TYPE, "application/json"); post.setEntity(new StringEntity(json, StandardCharsets.UTF_8)); } catch (Exception e) { e.printStackTrace(); } //执行请求并处理响应 CloseableHttpResponse response = null; try { response = httpClient.execute(post); HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity,"UTF-8"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //释放资源 try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
/**
* description: 发送http post 请求
* @param url
* @param String josn参数
* @param json headerInfo参数
* @return java.lang.String
*/
public static String doHttpPost(String url, String json, JSONObject headerInfo) {
//获取时间、流水
String time = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
int random = (int) (Math.random()*1000);
String serialNo = time + String.format("%03d",random);
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//创建HttpPost对象
HttpPost post = new HttpPost(url);
//设置POST请求传递参数
try {
//获取当前用户信息
String operatorID = headerInfo.getString("name");
String operatorName = headerInfo.getString("nickname");
//设置http请求头数据
post.setHeader("operatorID", operatorID);
post.setHeader("operatorName", URLEncoder.encode(operatorName,"utf-8"));
post.setHeader("reqSystemID", reqSystemID);
post.setHeader("reqSystemName", URLEncoder.encode(reqSystemName,"utf-8")); //解决中文乱码
post.setHeader("reqSystemDate", time.substring(0,8));
post.setHeader("reqSystemTime", time.substring(8,14));
post.setHeader("reqSystemSerialNO", serialNo);
post.setHeader(HTTP.CONTENT_TYPE, "application/json");
post.setEntity(new StringEntity(json, StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
//执行请求并处理响应
CloseableHttpResponse response = null;
try {
response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity,"UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
如有帮助希望点个推荐;如果没帮助到或者内容有错误,可以下面留言,谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!