java如何调用rest api接口
- 编程技术
- 2025-02-05 19:55:21
- 1
在Java中调用REST API接口,通常有以下几种方法: 1. 使用原生的HttpURLConnection类```javaimport java.io.Buffer...
在Java中调用REST API接口,通常有以下几种方法:
1. 使用原生的HttpURLConnection类
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class RestApiExample {
public static void main(String[] args) {
try {
URL url = new URL("http://example.com/api/resource");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
本文链接:http://www.xinin56.com/bian/477270.html
上一篇:电脑什么品牌的好些。