java 从网页读取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5232535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:06:57  来源:igfitidea点击:

Read data from webpage

javawebpage

提问by Robin clave

I am trying to read data from the given web page using Java.

我正在尝试使用 Java 从给定的网页中读取数据。

public class WebpageReader {
    public static void main(String[] args) throws IOException {
        String line = null, response;
        URL url = new URL("http://www.google.co.in/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                .getInputStream()));
        while (rd.readLine() != null) {
            line += rd.readLine();
        }
        System.out.println(line);

    }
}
public class WebpageReader {
    public static void main(String[] args) throws IOException {
        String line = null, response;
        URL url = new URL("http://www.google.co.in/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                .getInputStream()));
        while (rd.readLine() != null) {
            line += rd.readLine();
        }
        System.out.println(line);

    }
}

But I'm getting the connection refused exception. What might be correct way to get the date from webpage?

但我收到连接被拒绝的异常。从网页获取日期的正确方法是什么?

采纳答案by kgiannakakis

You are probably behind a proxy that doesn't allow you to connect to a web resource through a java application. You can configure a proxy in the java options. In Windows you can do that from the Control Panel.

您可能使用的代理不允许您通过 Java 应用程序连接到 Web 资源。您可以在 java 选项中配置代理。在 Windows 中,您可以从控制面板执行此操作。

回答by Nishant

You must be having proxy or firewall set. This code works.

您必须设置了代理或防火墙。此代码有效。