gpt4 book ai didi

java - 使用socket读取html并打印

转载 作者:行者123 更新时间:2023-12-01 06:25:24 25 4
gpt4 key购买 nike

我正在学习 Java,并且遇到了制作网址提取器的挑战。该程序什么也不做,只是扫描给它的页面以查找其中的外部链接。我有一个关于如何做到这一点的想法,但我在实现套接字类时遇到了一些麻烦。我需要帮助的是我需要创建一个套接字,它将使用端口 80 连接到 html 页面。接下来,我需要将该页面的完整 html 放入套接字的输入流中,以便我可以使用它进行操作以提取链接。

总而言之,我需要澄清以下事情:

  1. get html of the page into the input stream of the socket.
  2. print input stream onto the console.

编辑:对不起,我的错。输出流与输入流混淆。

最佳答案

您对套接字所做的事情尝试使用 UrlConnection 类。

    URL connection= new URL("http://lums.edu.pk);
URLConnection yc = connection.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();

如果您尝试使用套接字,您需要了解什么是 http 协议(protocol)和 how you can retrieve data从使用它的服务器。最后使用套接字发送 GET 请求。检查this问题来看看它是如何工作的。

关于java - 使用socket读取html并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28449985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com