java web----网络编程基础

OSI七层模型

InetAddress 简单使用

        InetAddress localHost = Inet4Address.getLocalHost();
        System.out.println(localHost.getHostAddress());
        System.out.println(localHost.getHostName());

        InetAddress byName = Inet4Address.getByName("123.56.138.186");
        System.out.println(byName);

URL使用

        URL url = new URL("http://www.baidu.com:80/index.html?uname=xx#a");
        System.out.println("协议: "+url.getProtocol());
        System.out.println("域名: "+url.getHost());
        System.out.println("端口: "+url.getPort());
        System.out.println("请求资源: "+url.getFile());
        System.out.println("请求资源: "+url.getPath());
        System.out.println("请求参数: "+url.getQuery());
        System.out.println("请求锚点: "+url.getRef());

模拟浏览器

    public static void main(String[] args) throws IOException {
        URL url = new URL("http://www.baidu.com:80/index.html?uname=xx#a");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("user-agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36");
        InputStream inputStream = conn.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "gbk"));
        String msg;
        while ((msg = bufferedReader.readLine())!=null){
            System.out.println(msg);
        }

    }

  

原文地址:https://www.cnblogs.com/yanxiaoge/p/11616809.html