java - http接口请求实现
文章目录
- java - http接口请求实现
- java调用http接口的常用的四种实现方式
- 1、通过JDK网络类Java.net.HttpURLConnection
- 2、通过common封装好的HttpClient
- 3、通过Apache封装好的CloseableHttpClient
- 4、通过SpringBoot-RestTemplate
java调用http接口的常用的四种实现方式
1、通过JDK网络类Java.net.HttpURLConnection;
2、通过common封装好的HttpClient;
3、通过Apache封装好的CloseableHttpClient;
4、通过SpringBoot-RestTemplate;
1、通过JDK网络类Java.net.HttpURLConnection
jdk自带的java.net包种,java.net.HttpURLConnection 支持对http的get,post请求
post请求:
public static String postHttpURL(String methodUrl,String parma) throws IOException {
HttpURLConnection connection = null;
OutputStream dataout = null;
BufferedReader reader = null;
String line = null;
try {
URL url = new URL(methodUrl);
connection = (HttpURLConnection) url.openConnection();// 根据URL生成HttpURLConnection
connection.setDoOutput(true);// 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true,默认情况下是false
connection.setDoInput(true); // 设置是否从connection读入,默认情况下是true;
connection.setRequestMethod("POST");// 设置请求方式为post,默认GET请求
connection.setUseCaches(false);// post请求不能使用缓存设为false
connection.setConnectTimeout(3000);// 连接主机的超时时间
connection.setReadTimeout(3000);// 从主机读取数据的超时时间
connection.setInstanceFollowRedirects(true);// 设置该HttpURLConnection实例是否自动执行重定向
connection.setRequestProperty("connection", "Keep-Alive");// 连接复用
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/json");
// connection.setRequestProperty("Authorization", "Bearer 66cb225f1c3ff0ddfdae31rae2b57488aadfb8b5e7");//安全验证
connection.connect();// 建立TCP连接,getOutputStream会隐含的进行connect,所以此处可以不要
// dataout = connection.getOutputStream();
dataout = new DataOutputStream(connection.getOutputStream());// 创建输入输出流,用于往连接里面输出携带的参数
dataout.write(parma.getBytes());
dataout.flush();
dataout.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 发送http请求
StringBuilder result = new StringBuilder();
// 循环读取流
while ((line = reader.readLine()) != null) {
result.append(line);//
}
return result.toString();
}
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
connection.disconnect();
}
return "";
}
总结使用结构:
//1.根据实际url地址创建URL实例对象;
//2.根据第1步URL实例对象创建HttpURLConnection实例对象;
//3.设置第二步获得HttpURLConnection实例对象的参数;(一些常用的请求参数配置)
//4.建立TCP连接,2种方式,对象直接调用connect()方法,或直接调用getOutputStream()方法(getOutputStream会隐含的进行connect)
//5.获取连接的输入流对象,将参数塞到输入流对象中
//6.获取状态码方法时,发送数据再获取接口返回的状态码
//7.获取HttpURLConnection实例对象的输出流,读取返回值数据
//8.关闭连接资源
/*
·HttpURLConnection对象不能直接构造,需要通过URL类中的openConnection()方法来获得。
·HttpURLConnection的connect()函数,实际上只是建立了一个与服务器的TCP连接,并没有实际发送HTTP请求。HTTP请求实际上直到我们获取服务器响应数据(如调用getInputStream()、getResponseCode()等方法)时才正式发送出去。
·对HttpURLConnection对象的配置都需要在connect()方法执行之前完成。
·HttpURLConnection是基于HTTP协议的,其底层通过socket通信实现。如果不设置超时(timeout),在网络异常的情况下,可能会导致程序僵死而不继续往下执行。
·HTTP正文的内容是通过OutputStream流写入的, 向流中写入的数据不会立即发送到网络,而是存在于内存缓冲区中,待流关闭时,根据写入的内容生成HTTP正文。
·调用getInputStream()方法时,返回一个输入流,用于从中读取服务器对于HTTP请求的返回信息。
·我们可以使用HttpURLConnection.connect()方法手动的发送一个HTTP请求,但是如果要获取HTTP响应的时候,请求就会自动的发起,比如我们使用HttpURLConnection.getInputStream()方法的时候,所以完全没有必要调用connect()方法。
*/
2、通过common封装好的HttpClient
3、通过Apache封装好的CloseableHttpClient
4、通过SpringBoot-RestTemplate