import java.net.*; import java.io.*; public class URLDemo { public static void main(String[] args) { try { //利用Java.net中的URL类来获取资源 URL url=new URL("https://www.baidu.com"); URLConnection urlConnection=url.openConnection(); HttpURLConnection connection=null; if(urlConnection instanceof HttpURLConnection) connection=(HttpURLConnection)urlConnection; else { System.out.println("请输入有效的URL地址"); return; } InputStreamReader isr=new InputStreamReader(connection.getInputStream(),"UTF-8"); BufferedReader in=new BufferedReader(isr); String current; while((current=in.readLine())!=null) { System.out.println(current); } } catch(IOException e) { e.printStackTrace(); } } }