Java - 使用Socket提取网站Html [英] Java - Website Html extracting using Socket

查看:103
本文介绍了Java - 使用Socket提取网站Html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码未从网址获取任何html

The code below doesn't get any html from the url

URL url = new URL("https://google.com");
        Socket socket = new Socket(url.getHost(),443);
        socket.setSoTimeout(15000);
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
        String c = "";
        while((c = reader.readLine())!=null)
        {
            System.out.print(c);
        }
        reader.close();
        socket.close();

我知道URLConnection方法openConnection()和URL类方法openStream()但我不知道我想用那些。所有我想知道的是为什么我没有使用Socket类获得任何输入,但我确实得到了使用其他方法的输入。

I'm aware of URLConnection method openConnection() and URL class method openStream() but i don't want to use those. All i want to know is why i don't get any input using Socket class but i do get an input using other approaches.

推荐答案

您没有发送任何请求。 HTTP是一种请求 - 响应协议:您需要发送一个请求,告诉Web服务器您要检索哪个URL,然后它会向您发送此URL的数据。

You're not sending any request. HTTP is a request-response protocol: you need to send a request telling the Web server which URL you want to retrieve, and then it will send you the data for this URL.

这篇关于Java - 使用Socket提取网站Html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆