Read from URL java

后端 未结 4 1249
抹茶落季
抹茶落季 2021-01-03 00:57

I\'m trying to read URL in java, and it works as long as the URL is loading in browser.

But if it is just cylcing in the browser and not loading that page when I\'m

4条回答
  •  执笔经年
    2021-01-03 01:18

    The URL#openStream method is actually just a shortcut for openConnection().getInputStream(). Here is the code from the URL class:

    public final InputStream openStream() throws java.io.IOException {
      return openConnection().getInputStream();
    }
    
    • You can adjust settings in the client code as follows:

      URLConnection conn = url.openConnection();
      // setting timeouts
      conn.setConnectTimeout(connectTimeoutinMilliseconds);
      conn.setReadTimeout(readTimeoutinMilliseconds);
      InputStream in = conn.getInputStream();
      

    Reference: URLConnection#setReadTimeout, URLConnection#setConnectTimeout

    • Alternatively, you should set the sun.net.client.defaultConnectTimeout and sun.net.client.defaultReadTimeout system property to a reasonable value.

提交回复
热议问题