怎么发送一个GET请求到一个服务器
时间: 2023-08-05 11:05:36 浏览: 35
要发送一个GET请求到一个服务器,需要使用HTTP协议。以下是使用Java语言发送GET请求的示例代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpGetRequest {
public static void main(String[] args) {
try {
URL url = new URL("http://www.example.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
该代码会向"http://www.example.com"发送一个GET请求,并打印出服务器返回的响应内容。你可以将URL替换成你想要请求的服务器地址。