java使用socket读取网页
Posted 悉野小楼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java使用socket读取网页相关的知识,希望对你有一定的参考价值。
/** * 使用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 Host: " + strHost + " Connection: Close "; 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 + " "); } //要关闭各种 bufferedReader.close(); inputStreamReader.close(); inputStream.close(); outputStream.close(); socket.close(); } catch (Exception ex) { } }
以上是关于java使用socket读取网页的主要内容,如果未能解决你的问题,请参考以下文章
solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例
Java中使用Socket连接判断Inputstream结束,java tcp socket服务端,python tcp socket客户端