用java写一个代码,用来获取网页的url
时间: 2023-05-17 13:04:43 浏览: 99
lucene,java获取网页源码,b2b源码java
可以使用 Java 的 HttpURLConnection 类来获取网页的 URL。以下是一个简单的示例代码:
```java
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class GetURL {
public static void main(String[] args) {
try {
URL url = new URL("https://www.example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
```
这个代码会打印出 https://www.example.com 网页的内容。
阅读全文