zoukankan      html  css  js  c++  java
  • 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);
            }
    
        }
    

      

  • 相关阅读:
    redis命令
    linux命令行任务管理
    tomcat修改内存
    Python调用shell
    取消myeclipse自动进入workspace
    解决Myeclipse编译不生成.class文件问题
    Manacher回文串算法学习记录
    青少年如何使用 Python 开始游戏开发
    对 Linux 专家非常有用的 20 个命令
    对中级 Linux 用户非常有用的 20 个命令
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/11616809.html
Copyright © 2011-2022 走看看