Java URL 处理

说明

Java URLConnection getHeaderFieldLong(String name, long Default) 方法返回解析为 long 的命名字段的值。

声明

以下是 java.net.URLConnection.getHeaderFieldLong(String name, long Default) 方法的声明

public long getHeaderFieldLong(String name, long Default) 

    参数

    name - 标头字段的名称。

    default - 默认值。

    返回值

    字段的值,解析为长整型。如果字段丢失或格式错误,则返回默认参数的值。

    异常

    示例 1

    以下内容示例显示了使用 Java URLConnection getHeaderFieldLong(String name, long Default) 方法获取 https 协议的有效 url。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 URLConnection 实例。使用 getHeaderFieldLong(String name, long Default),我们获取由名称 Expires 标识的标头字段的值并打印相应的值 -

    package com.yxjc123;
    
    import java.io.IOException;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class UrlConnectionDemo {
       public static void main(String [] args) {
          try {
             URL url = new URL("https://www.yxjc123.com");
             URLConnection urlConnection = url.openConnection();
             long headerValue = urlConnection.getHeaderFieldLong("Content-Length",1L);
             System.out.println("Content-Length: " + headerValue);
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

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

    输出

    Content-Length: 293827 

      示例 2

      以下示例显示了有效 url 的 Java URLConnection getHeaderFieldLong(String name, long Default) 方法的用法与http协议。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 URLConnection 实例。使用 getHeaderFieldLong(String name, long Default),我们获取由名称 Date 标识的标头字段的值并打印相应的值 -

      package com.yxjc123;
      
      import java.io.IOException;
      import java.net.URL;
      import java.net.URLConnection;
      
      public class UrlConnectionDemo {
         public static void main(String [] args) {
            try {
               URL url = new URL("http://www.yxjc123.com");
               URLConnection urlConnection = url.openConnection();
               long headerValue = urlConnection.getHeaderFieldLong("Content-Length",1L);
               System.out.println("Content-Length: " + headerValue);
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      } 
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17

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

      输出

      Content-Length: 0 

        示例 3

        以下示例显示了有效 url 的 Java URLConnection getHeaderFieldLong(String name, long Default) 方法的用法与http协议。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 URLConnection 实例。使用 getHeaderFieldLong(String name, long Default),我们获取由名称 Expires 标识的标头字段的值并打印相应的值 -

        package com.yxjc123;
        
        import java.io.IOException;
        import java.net.URL;
        import java.net.URLConnection;
        
        public class UrlConnectionDemo {
           public static void main(String [] args) {
              try {
                 URL url = new URL("http://www.baidu.com");
                 URLConnection urlConnection = url.openConnection();
                 long headerValue = urlConnection.getHeaderFieldLong("Content-Length",1L);
                 System.out.println("Content-Length: " + headerValue);
              } catch (IOException e) {
                 e.printStackTrace();
              }
           }
        } 
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17

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

        输出

        Content-Length: 1