码迷,mamicode.com
首页 > 编程语言 > 详细

java使用socket读取网页

时间:2020-06-02 11:32:18      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:address   exception   webp   byte   div   reader   关闭   bsp   new   

/**
     * 使用socket读取网页内容
     */
    private static void socketReadWebPage() {
        try {
            String strHost = "www.baidu.com";
            InetSocketAddress socketAddress = new InetSocketAddress(strHost, 80);
            Socket socket = new Socket();
            socket.connect(socketAddress);
            String strSend = "GET / HTTP/1.1\r\nHost: " + strHost + "\r\nConnection: Close\r\n\r\n";
            OutputStream outputStream = socket.getOutputStream();
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = strSend.getBytes();
            outputStream.write(bytes);
            outputStream.flush();

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while((line = bufferedReader.readLine()) != null) {
System.out.println(line
+ "\r\n"); } //要关闭各种 bufferedReader.close(); inputStreamReader.close(); inputStream.close(); outputStream.close(); socket.close(); } catch (Exception ex) { } }

 

java使用socket读取网页

标签:address   exception   webp   byte   div   reader   关闭   bsp   new   

原文地址:https://www.cnblogs.com/barrysgy/p/13029646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!