_ Network sockets _java

Since 1 ( https://www.cnblogs.com/S-Mustard/p/11924227.html ) mentioned access via telnet Baidu home page, now enable access Baidu services via java code to get home information.

 

       try(Socket s=new Socket("www.baidu.com",80);
            Scanner in =new Scanner(s.getInputStream(),"UTF-8");
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));){
            StringBuffer sb = new StringBuffer();
            sb.append("GET https://www.baidu.com HTTP/1.1\r\n").append("Host:"+ "www.baidu.com" + "\r\n").append("\r\n");
            out.write(sb.toString());
            out.flush();
            while(in.hasNextLine()){
                System.out.println(in.nextLine());
            }
        }

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/S-Mustard/p/11938744.html