InputStreamReader不从某些网站读取HTML代码

发布于 2025-02-02 13:56:35 字数 647 浏览 1 评论 0原文

我使用以下代码

if (HttpURLConnection.HTTP_OK == con.getResponseCode()) {
                BufferedReader bf = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String line;
                while ((line = bf.readLine()) != null) {
                    sb.append(line);
                    sb.append("/n");
                }
                System.out.println(sb);
            }

从我的连接中读取HTML代码。代码效果很好,并且在某些网站上得到了正确的事情,但是其他网站似乎对此有所防御?无论哪种方式,我都不会收到完整的HTML连接代码。 这是应该打印出的实际结果

I use the following piece of code

if (HttpURLConnection.HTTP_OK == con.getResponseCode()) {
                BufferedReader bf = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String line;
                while ((line = bf.readLine()) != null) {
                    sb.append(line);
                    sb.append("/n");
                }
                System.out.println(sb);
            }

to read HTML code from my connection. Code works just fine and gets the right thing on some websites, but other ones seem to have some kind of defence against this? Either way, I don't receive full HTML code of connection.
Here's an example of what should be printed out expected result vs what I get actual result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2025-02-09 13:56:36

一些Web服务器要求您指定用户代理,在这里您可以找到它们的列表: https://user-agents.net/

您可以尝试使用Windows 10(64bit)上的Firefox 100.0之类的东西:

URL url = new URL(/* ... */);
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0 Viewer/93.9.7218.19");

Some web servers require you to specify a user agent, here you can find a list of them: https://user-agents.net/

You could try something like Firefox 100.0 on Windows 10 (64bit):

URL url = new URL(/* ... */);
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0 Viewer/93.9.7218.19");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文