• 获取URL相关信息


    import java.net.URL;
    import java.net.URLConnection;
    import java.net.ConnectException;
    import java.io.IOException;
    import java.util.Date;
    import java.net.HttpURLConnection;
    
    public class GetURLInfo{
        public static void printInfo(URL url){
            System.out.println("==================URL info==================");
            //URL基本信息
            System.out.println("File ["+url.getFile()+"]");
            System.out.println("Protocol ["+url.getProtocol()+"]");
            System.out.println("Authority ["+url.getAuthority()+"]");
            System.out.println("DefaultPort ["+url.getDefaultPort()+"]");
            System.out.println("Host ["+url.getHost()+"]");
            System.out.println("Path ["+url.getPath()+"]");
            System.out.println("Port ["+url.getPort()+"]");
            System.out.println("Query ["+url.getQuery()+"]");
            System.out.println("Ref ["+url.getRef()+"]");
            System.out.println("UserInfo ["+url.getUserInfo()+"]");
            
            URLConnection c = null;
            try{
                c = url.openConnection();
                System.out.println("==================URLConnection info==================");
                //显示信息
                System.out.println("ContentType [" + c.getContentType() + "]");
                System.out.println("ContentEncoding [" + c.getContentEncoding() + "]");
                System.out.println("Content [" + c.getContent() + "]");
                System.out.println("ContentLength [" + c.getContentLength() + "]");
                System.out.println("Date [" + new Date(c.getDate()) + "]");
                System.out.println("LastModified [" + new Date(c.getExpiration()) + "]");
                System.out.println("Expiration [" + new Date(c.getLastModified()) + "]");
            
                if(c instanceof HttpURLConnection){
                    System.out.println("==================HttpURLConnection info==================");
                    HttpURLConnection httpURL = (HttpURLConnection)c;
                    System.out.println("RequestMethod [" + httpURL.getRequestMethod() + "]");
                    System.out.println("ResponseMessage [" + httpURL.getResponseMessage() + "]");
                    System.out.println("ResponseCode [" + httpURL.getResponseCode() + "]");
                    }
                }catch(IOException e){
                e.printStackTrace();
                }
            }
            
        public static void main(String[] args){
            try{
                String urlStr = "http://www.sina.com.cn:80/index.html";
                URL url = new URL(urlStr);
                printInfo(url);
            }catch(Exception e){
                System.err.println(e);
                }
            }
        }

    运行结果:

    G:maul keyboard etwork programming>javac GetURLInfo.java

    G:maul keyboard etwork programming>java GetURLInfo
    ==================URL info==================
    File [/index.html]
    Protocol [http]
    Authority [www.sina.com.cn:80]
    DefaultPort [80]
    Host [www.sina.com.cn]
    Path [/index.html]
    Port [80]
    Query [null]
    Ref [null]
    UserInfo [null]
    ==================URLConnection info==================
    ContentType [text/html]
    ContentEncoding [null]
    Content [sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6178b9ce]
    ContentLength [21826]
    Date [Sat Sep 08 18:29:00 GMT+08:00 2018]
    LastModified [Sat Sep 08 18:28:54 GMT+08:00 2018]
    Expiration [Wed Apr 27 10:00:04 GMT+08:00 2005]
    ==================HttpURLConnection info==================
    RequestMethod [GET]
    ResponseMessage [OK]
    ResponseCode [200]

    G:maul keyboard etwork programming>

  • 相关阅读:
    教你用photoshop cs5或者cs6做IPad,背景随意换,gif制作,高清教程,原创
    ASP.NET MVC4 IN ACTION学习笔记第一波
    潜移默化学会C#不常用语法《1》动态类型绑定dynamic
    SubSnoic 框架入门到提高(1)全程记录
    杨洋疯狂C# 刊号:201208 第1期ASPNET验证(一)
    杨洋疯狂C# 刊号:201207 第1期
    ASP.NET MVC4 IN ACTION学习笔记第二波
    JavaScript深入【表达式和运算符(上集)】你能过我8关js运算符的题目吗?
    清新空气我的.net(C#)生涯知识总结 跨CSS,JS,JAVA,AJAX,WPF,WCF,LINQ,ASP.NET,Winform,Sqlserver,Mysql,EF,OOP,开发工具等
    潜移默化学会WPF(Treeview异步加载节点)
  • 原文地址:https://www.cnblogs.com/celine/p/9610005.html
Copyright © 2020-2023  润新知