java实现http请求
时间: 2023-09-02 09:08:48 浏览: 98
好的,关于java实现http请求,您可以使用Java自带的HttpURLConnection类或者第三方库比如Apache的HttpClient来实现。以下是一个用HttpURLConnection发送GET请求的示例代码:
```
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String[] args) {
try {
URL url = new URL("http://example.com/api"); // 替换成您要请求的url
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
希望这个代码示例可以帮到您!
阅读全文