【问题标题】:unable to read the data无法读取数据
【发布时间】:2017-11-12 17:35:29
【问题描述】:

我正在尝试从我也可以访问的特定 URL 读取内容。我遇到了问题。所以我只是从here的基本示例开始

我正面临 ConnectionException。

public class UrlReader {
    public static void main(String[] args) throws Exception {

            System.setProperty("http.proxyHost","http://www.oracle.com/");
            System.setProperty("http.proxyPort", "8081");

        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication("doaminName\\username","password".toCharArray());
            }
        });

        URL url = new URL("http://www.oracle.com/");
        URLConnection con = url.openConnection();

        BufferedReader in = new BufferedReader(new InputStreamReader(
                con.getInputStream()));

        // Read it ...
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);

        in.close();


       /* URL oracle = new URL("http://www.oracle.com/");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();*/
    }
}

例外:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)

我已经按照一些在线链接来解决它,但没有成功。 是否有人在使用 工作笔记本电脑 工作时遇到过此类问题。我想连接到某个 URL 并使用 java 从该 URL 读取内容。

【问题讨论】:

  • 你试过https://www.oracle.com/吗?该页面是 https。
  • 是的,线程“主”java.net.ConnectException 中的相同异常异常:连接被拒绝:连接。这是因为我正在尝试使用可能有防火墙的工作笔记本电脑(不确定)。 @GlenPierce
  • www.oracle.com 不是 HTTP 代理主机,www.oracle.com:8081 不是 HTTP 代理端口。尝试使用自己作为代理连接到主机没有任何意义。 “连接被拒绝”并不意味着“无法读取数据”。你的问题没有意义。

标签: java inputstream java-io


【解决方案1】:
System.setProperty("http.proxyHost","http://www.oracle.com/");
System.setProperty("http.proxyPort", "8081");

Oracle 不向外界提供端口 8081 上的 HTTP 代理服务,或者可能根本不提供。这两行代码毫无意义,尤其是当您的目标 URL 也是 www.oracle.com 时。删除它们。

【讨论】:

  • 如果您在我的帖子中看到我的代码,最初我尝试使用未使用 System.setProperty 的注释代码。我遇到了同样的问题。那是因为一些防火墙吗??
  • 真的吗?同样的问题?拒绝连接?到我可以从此浏览器访问的公共 Web 服务器?我不这么认为。除非是你的自己的防火墙挡道。
  • 是的,我相信它是我自己的防火墙,因为我正在使用公司的(工作)笔记本电脑。有没有办法解决这个问题?
  • 很可能您的公司提供了 HTTP 代理。找出它是什么并将相关的系统属性设置为它。没有到达目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 2017-07-07
  • 2016-07-30
相关资源
最近更新 更多