多用户发起java后端get请求
时间: 2023-03-19 16:23:57 浏览: 78
要让多个用户发起Java后端GET请求,您可以使用Java中的HttpURLConnection类来发送GET请求。下面是一个示例代码:
```java
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MultipleUserGetRequest {
public static void main(String[] args) throws Exception {
String url = "https://www.example.com/api"; // 要发送GET请求的API地址
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
System.out.println("发送GET请求到URL:" + url);
System.out.println("响应码:" + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("响应内容:" + response.toString());
}
}
```
以上代码中,您可以将`url`变量替换为您要发送GET请求的API地址,然后使用多线程或其他适当的方法,将此代码用于多个用户发起GET请求。请注意,如果您要在多线程环境下使用此代码,请确保在多线程中创建新的`HttpURLConnection`对象,否则可能会遇到线程安全问题。