前言
我们调用接口,通常都是在微服务中,可以直接使用client,通过feign调用别的服务的接口,
还有一种原始的方法。如下:
1.核心工具类
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
/**
* Http请求工具类
* @author weihong.zhu
*/
@Component
public class HttpUtil {
/**
* 设置连接超时时间,20000,消除魔术数字
*/
private static final int TWOTHOUSAND = 20000;
/**
* 发送post请求
*
* @param url 请求路径
* @param header 请求头
* @param body 请求体
* @return String
*/
public String doPost(String url, Map<String, String> header, String body) {
String result = "";
BufferedReader in = null;
PrintWriter out = null;
try {
// 设置 url
URL realUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
// 设置 header
for (String key : header.keySet()) {
connection.setRequestProperty(key, header.get(key));
}
// 设置请求 body
connection.setDoOutput(true);
connection.setDoInput(true);
//设置连接超时和读取超时时间
connection.setConnectTimeout(TWOTHOUSAND);
connection.setReadTimeout(TWOTHOUSAND);
try {
out = new PrintWriter(connection.getOutputStream());
// 保存body
out.print(body);
// 发送body
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
try {
// 获取响应body
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
//return null;
}
return result;
}
/**
* 发送get请求
*
* @param url 请求路径
* @param header 请求头
* @return String
*/
public String doGet(String url, Map<String, String> header) {
String result = "";
BufferedReader in = null;
try {
// 设置 url
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
// 设置 header
for (String key : header.keySet()) {
connection.setRequestProperty(key, header.get(key));
}
// 设置请求 body
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
return null;
}
return result;
}
}
2.使用
Map<String, String> map = new HashMap<>(); // 所有的参数,通过map形式放入header中
String jsonStr = httpUtil.doGet(url, map); //返回jsonstring 形式的结果