• 获得URl信息


     
    public class GetUrlInfo
    {
        public static void printfInfo(URL url)throws Exception
        {
            //基本信息
            System.out.println("文件        "+url.getFile());
            System.out.println("Protocol        "+url.getProtocol());//获取URL的协议名称
            System.out.println("host        "+url.getHost());
            System.out.println("端口        "+url.getPort());//获取URl的端口号
            System.out.println("路径        "+url.getPath());//获取URL的路径部分
     
     
            URLConnection c=url.openConnection();
            c.connect();
            System.out.println("Contend type        "+c.getContentType());
            System.out.println("Contend Encoding        "+c.getContentEncoding());
            System.out.println("Contend Length        "+c.getContentLength());
            System.out.println("Date        "+new Date(c.getDate()));
            System.out.println("Date        "+new Date(c.getLastModified()));
            System.out.println("Expriration        "+new Date(c.getExpiration()));
     
            if(c instanceof HttpURLConnection)
            {
                HttpURLConnection h=(HttpURLConnection) c;
                System.out.println(" reust Method        "+h.getRequestMethod());
                System.out.println(" Resonse Message        "+h.getResponseMessage());
                System.out.println(" Resonse Code         "+h.getResponseCode());
            }
     
     
     
        }
     
        public static void vist(URL url)throws Exception//抓取某网站的内容
        {
     
            File file=new File("C:/hhh2.txt");
            String tempString;
            StringBuffer sb=new StringBuffer();
             //InputStream in=new InputStreamReader(url.openStream())
            BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream()));
            BufferedWriter out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            while((tempString=in.readLine())!=null)
            {
                //out.write(tempString);
                //sb.append(tempString);
                System.out.println(tempString);
            }
            in.close();
            out.close();
            System.out.println(sb.toString());
        }
        public static void main(String []args)
        {
            String urlString="https://www.zhihu.com/question/48629658";
            try
            {
                URL url=new URL(urlString);
                System.out.println(url.getUserInfo());
                vist(url);
                //printfInfo(url);
            } catch (Exception e)
            {
                // TODO: handle exception
            }
     
        }
    }
    梦里不知身是客,一晌贪欢。
  • 相关阅读:
    [转] ArcGIS自定义工具完全教程 1/2
    ArcMap工具栏中嵌入自定义按钮
    自定义ArcGIS应用程序
    制作ArcGIS DLL插件的安装包
    如何编写和注册.Net的Com组件
    空间参考(三)ArcGIS Engine的支持
    ArcGIS Desktop开发基础
    ArcGIS中坐标系统简介
    Ao开发
    ArcGIS Desktop Addin插件开发系列
  • 原文地址:https://www.cnblogs.com/dccmmtop/p/5710309.html
Copyright © 2020-2023  润新知