Java URL 处理

说明

Java URL getContent() 方法返回此 URL 的内容。

openConnection().getContent() 

    声明

    以下是java.net.URL.getContent()方法的声明

    public final Object getContent() 

      参数

      返回值

      返回 URL 的内容。

      异常

      IOException - 如果发生 I/O 异常。

      示例 1

      以下示例显示如何使用 Java URL getContent() 方法获取有效的 url。在此示例中,我们将创建 URL 类的实例。现在使用 getContent() 方法,我们获取内容并打印其类型 

      package com.yxjc123;
      
      import java.io.IOException;
      import java.net.URL;
      
      public class UrlDemo {
         public static void main(String [] args) {
            try {
               URL url = new URL("https","www.yxjc123.com","/index.htm");
               Object content = url.getContent();
               System.out.println(content);
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      } 
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

      让我们编译并运行上面的程序,这将产生以下结果 -

      输出

      sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6df97b55 

        示例 2

        以下示例显示了针对无效 URL 的 Java URL getContent() 方法的用法。在此示例中,我们将创建 URL 类的实例。现在使用 getContent() 方法,我们尝试获取内容,并引发异常并打印 

        package com.yxjc123;
        
        import java.io.IOException;
        import java.net.URL;
        
        public class UrlDemo {
           public static void main(String [] args) {
              try {
                 URL url = new URL("https","www.yxjc123.com","index.htm");
                 Object content = url.getContent();
                 System.out.println(content);
              } catch (IOException e) {
                 e.printStackTrace();
              }
           }
        } 
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15

        让我们编译并运行上面的程序,这将产生以下结果 -

        输出

        java.io.IOException: Server returned HTTP response code: 400 for URL: https://www.yxjc123.comindex.htm
        	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
        	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
        	at java.net.URLConnection.getContent(Unknown Source)
        	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(Unknown Source)
        	at java.net.URL.getContent(Unknown Source)
        	at com.yxjc123.UrlDemo.main(UrlDemo.java:10) 
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6

        示例 3

        以下示例显示如何使用 Java URL getContent() 方法获取有效的 url。在此示例中,我们使用协议、主机和文件名创建 URL 类的实例。现在使用 getContent() 方法,我们获取内容并打印其类型,然后使用 openConnection() 方法,我们获取内容长度并将其打印在输出中,如下所示 

        package com.yxjc123;
        
        import java.io.IOException;
        import java.net.URL;
        
        public class UrlDemo {
           public static void main(String [] args) {
              try {
                 URL url = new URL("https","www.yxjc123.com","/index.htm");
                 Object content = url.getContent();
                 System.out.println(content.getClass());
                 System.out.println(url.openConnection().getContentLength());
              } catch (IOException e) {
                 e.printStackTrace();
              }
           }
        } 
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16

        让我们编译并运行上面的程序,这将产生以下结果 

        输出

        class sun.net.www.protocol.http.HttpURLConnection$HttpInputStream
        293637 
        • 1