java web如何调用接口
- 编程技术
- 2025-01-25 20:48:18
- 1

在Java Web中调用接口主要有以下几种方式: 1. 使用`HttpURLConnection`Java内置的`HttpURLConnection`类可以用来发送HT...
在Java Web中调用接口主要有以下几种方式:
1. 使用`HttpURLConnection`
Java内置的`HttpURLConnection`类可以用来发送HTTP请求。
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionExample {
public static void main(String[] args) {
try {
URL url = new URL("http://example.com/api/data");
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/340895.html
上一篇:vivo个人热点在哪里开
下一篇:如何用数据线连接手机