網絡_套接字_java

1  因爲(http://www.javashuo.com/article/p-csyaavyd-gu.html)講到經過telnet訪問百度首頁,如今經過java代碼來實現訪問百度服務,獲取首頁信息。html

 

       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());
            }
        }

 

 

相關文章
相關標籤/搜索