zoukankan      html  css  js  c++  java
  • URL以及URLConnection对象

    java中既然对ip地址都进行了对象的封装,那必须对URL对象进行封装

    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class URLDemo {
    
        public static void main(String[] args) throws Exception {
            
            //解析url中的数据,使用URL对象
            String str_url = "http://192.168.17.1:8080/myapp/font.html?name=lisi";
            
            URL url = new URL(str_url);
    //        System.out.println("getProtocol:"+url.getProtocol());
    //        System.out.println("getHost:"+url.getHost());
    //        System.out.println("getPort:"+url.getPort());
    //        System.out.println("getPath:"+url.getPath());
    //        System.out.println("getFile:"+url.getFile());
    //        System.out.println("getQuery:"+url.getQuery());
            
            //通过openConnection();获取到远程资源的连接对象
            URLConnection conn = url.openConnection();//内置Socket
            System.out.println(conn);
            
            //调用连接对象的读取方法,准备读取资源
            InputStream in = conn.getInputStream();
            byte[] buf = new byte[1024];
            int len = in.read(buf);
            String str = new String(buf,0,len);
            System.out.println(str);
            
        }
    
    }
  • 相关阅读:
    proguard-rules.pro、混淆、导jar包
    百度地图相关开发
    开发apicloud模块遇到的几个梗
    Android相关概念
    file-downloader相关问题
    Android 线程
    Android Studio Tip of the Day
    NAudio的使用说明
    IT回忆录-2
    IT回忆录-1
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5932694.html
Copyright © 2011-2022 走看看