java写一个http,可以get
时间: 2024-04-02 15:31:29 浏览: 74
java写的http下载
好的,以下是一个简单的 Java HTTP GET 请求示例代码:
```java
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class HttpGetExample {
public static void main(String[] args) throws Exception {
String url = "http://www.example.com";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方法为 GET
con.setRequestMethod("GET");
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` 变量替换成你需要请求的 URL,运行该程序即可发送 GET 请求并获取响应结果。
阅读全文