Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java URL在网站中挂起且从不读取_Java_Url - Fatal编程技术网

Java URL在网站中挂起且从不读取

Java URL在网站中挂起且从不读取,java,url,Java,Url,我试图在一个网站上阅读并将其保存为字符串。下面我使用的代码在Eclipse中运行得非常好。但是,当我尝试在Windows中通过命令行运行该程序(如“JavaMyProgram”)时,该程序会启动并挂起,并且从不读取URL。有人知道为什么会这样吗 URL link = new URL("http://www.yahoo.com"); BufferedReader in = new BufferedReader(new InputStreamReader(link.open

我试图在一个网站上阅读并将其保存为字符串。下面我使用的代码在Eclipse中运行得非常好。但是,当我尝试在Windows中通过命令行运行该程序(如“JavaMyProgram”)时,该程序会启动并挂起,并且从不读取URL。有人知道为什么会这样吗

URL link = new URL("http://www.yahoo.com");

            BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
            //InputStream in = link.openStream();
            String inputLine = "";
            int count = 0;
            while ((inputLine = in.readLine()) != null)
            {
                site = site + "\n" + inputLine;
            }
            in.close();

如果你的代码就是这么做的,那么没有理由不在命令行中工作。我怀疑你把坏掉的东西剪掉了。例如:

public static void main(String[] args) throws Exception {
    String site = "";
    URL link = new URL("http://www.yahoo.com");
    BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
    //InputStream in = link.openStream();
    String inputLine = "";
    int count = 0;
    while ((inputLine = in.readLine()) != null) {
        site = site + "\n" + inputLine;
    }
    in.close();
    System.out.println(site);
}

很好。另一种可能是,如果您在Eclipse中运行它,并且在两台不同的计算机上通过命令行运行,而后者无法访问。

这可能是因为您在代理之后,而Eclipse会自动在其中添加设置来配置它


如果您在代理之后,在命令提示符下运行时,请尝试设置
java.net.useSystemProxies
属性。您还可以手动配置代理设置,并找到一些网络属性(
http.proxyHost
http.proxyPort
)。

我遇到了这样的问题并找到了解决方案。 这里是我的工作代码:

// Create a URL for the desired page
URL url = new URL("your url");
// Get connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000); // 5 seconds connectTimeout
connection.setReadTimeout(5000 ); // 5 seconds socketTimeout

// Connect
connection.connect(); // Without this line, method readLine() stucks!!!
// because it reads incorrect data, possibly from another memory area

InputStreamReader isr = new InputStreamReader(url.openStream(),"UTF-8");
BufferedReader in = new BufferedReader(isr);
String str;

while (true) {
   str = in.readLine();
   if(str==null){break;}
   listItems.add(str);
}

// Closing all
in.close();
isr.close();
connection.disconnect();

可能有什么东西限制了程序访问internet。尝试通过命令行ping一个网站。+1-注意Eclipse不一定使用与从命令行运行的Java应用程序相同的代理。我尝试设置System.setProperty(“Java.net.useSystemProxies”,“true”);在代码中,但这似乎没有任何作用。一些解释它是如何工作的将有助于了解问题的原因。超时是一个好主意。顺便问一句,你认为一个程序是否有可能被你的示例代码无休止地陷入while循环?