原生java发送http请求
package com.cyz;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* @author cyz
* @since 2023/11/8 9:50
*/
public class CustHttp {
public static String post(String targetUrl, String params) throws IOException {
URL url = new URL(targetUrl);
PrintWriter out = null;
BufferedReader in = null;
try {
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Request-Origion", "SwaggerBootstrapUi");
urlConnection.setRequestProperty("accept", "*/*");
urlConnection.setRequestProperty("Content-Type", "application/json");
//post请求要使用下面两个
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
out = new PrintWriter(urlConnection.getOutputStream());
out.write(params);
out.flush();
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String res = "";
String line = "";
while ((line = in.readLine()) != null) {
res += line;
}
return res;
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
}
public static String get(String targetUrl, String token) throws IOException {
URL url = new URL(targetUrl);
BufferedReader in = null;
try {
URLConnection connection = url.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Authorization", token);
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String res = "";
String line = "";
while ((line = in.readLine()) != null) {
res += line;
}
return res;
} finally {
if (in != null) {
in.close();
}
}
}
public static void main(String[] args) throws IOException {
String post = post("http://10.21.1.154:19500/api/v1/user/login", "{\n" +
"\t\"captcha\": \"1\",\n" +
"\t\"code\": \"1\",\n" +
"\t\"password\": \"123456\",\n" +
"\t\"username\": \"superAdmin\"\n" +
"}");
System.out.println(post);
String s = get("http://10.21.1.154:19500/api/v1/user/getBaseInfo?opId=1808",
"/bv1CXBJczpJm6d46e+KkT2qNC00Bd9L/b9Y4Awq5P3dEaQySoJP1uCOG3uP9ubydsBSCeD/UsUoz" +
"7ZHTOMeNCQtEuEYeu82NV4QahqWJ8zcDgPpC5ev+wII7ApYK/z54mrfvKW2/A4ferRB4EJXuwfzoRfm3RRh" +
"THTiMnRuLY1BqUZP+3rCpcinWpeCpZVGEPnImU4ZDmUNtOG4pn0Dexk3UMp838a+sgkXcRJKLCcvQd0i2Jez7MiJ" +
"iQrGedZVN6XXiPViETRV7NQNhBQCqZO4C9P+c9/tBuY2Qq6WrZhNhxJvphYiaIuG+zyjL+ZkYQgeKeVo6waecCnnLoBZV2" +
"nPmbbAmiqCrmNfueJG3wv/lBvVyKg+C1n1rjKnTIxkmVbC8v44TwBY3gi6HaBf4IFxf/hoiBgv1WqJZfPf4GpL6f2e0nhN" +
"s4ho5M6jQTmmjAKOei8P3FtaIogvsc9HPg==");
System.out.println(s);
}
}
使用Hutool工具类
依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.18</version>
</dependency>
HttpRequest
HttpResponse getBaidu = HttpRequest.get("www.baidu.com")
.execute();
System.out.println(getBaidu.body());
HttpResponse execute = HttpRequest.post("http://10.21.1.154:19500/api/v1/user/login")
.header("Request-Origion", "SwaggerBootstrapUi")
.header("accept", "*/*")
.header("Content-Type", "application/json")
.body("{\n" +
"\t\"captcha\": \"1\",\n" +
"\t\"code\": \"1\",\n" +
"\t\"password\": \"123456\",\n" +
"\t\"username\": \"superAdmin\"\n" +
"}").execute();
System.out.println(execute.body());
HttpUtil
String s = HttpUtil.get("www.baidu.com");
System.out.println(s);
HttpResponse execute = HttpUtil.createPost("http://10.21.1.154:19500/api/v1/user/login")
.header("Request-Origion", "SwaggerBootstrapUi")
.header("accept", "*/*")
.header("Content-Type", "application/json")
.body("{\n" +
"\t\"captcha\": \"1\",\n" +
"\t\"code\": \"1\",\n" +
"\t\"password\": \"123456\",\n" +
"\t\"username\": \"superAdmin\"\n" +
"}").execute();
System.out.println(execute.body());
resttemplate
(省略)