Is it necessary to use url.openConnection ()?

As we all know, both the code will lead to the same result

public class MainApp { public static void main(String[] args) throws IOException { URL google = new URL("http://www.google.com"); google.openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(google.openStream())); reader.lines().forEach(System.out::println); } } 

and

 public class MainApp { public static void main(String[] args) throws IOException { URL google = new URL("http://www.google.com"); BufferedReader reader = new BufferedReader(new InputStreamReader(google.openStream())); reader.lines().forEach(System.out::println); } } (google.openStream ())); public class MainApp { public static void main(String[] args) throws IOException { URL google = new URL("http://www.google.com"); BufferedReader reader = new BufferedReader(new InputStreamReader(google.openStream())); reader.lines().forEach(System.out::println); } } ).; public class MainApp { public static void main(String[] args) throws IOException { URL google = new URL("http://www.google.com"); BufferedReader reader = new BufferedReader(new InputStreamReader(google.openStream())); reader.lines().forEach(System.out::println); } } 

So what's the point when you use google.openConnection()?

+5
source share
3 answers

Maybe javadoc for this method helps:

public java.net.URLConnection openConnection() throws java.io.IOException

It returns an instance URLConnection , which represents a connection to a remote object referenced by the URL . A new instance of URLConnection is created every time you call URLStreamHandler.openConnection(URL) handler method protocol for this URL.

It should be noted that an instance of URLConnection does not establish a network connection with the actual establishment. This will happen only when the call URLConnection.connect() .

If the protocol URL (for example, HTTP or JAR) there is a public, specialized subclasses of URLConnection , belonging to one of the following packages or one of their subpackages: java.lang , java.io , java.util , java.net , the connection will be returned to this subclass. For example, for HTTP returns HttpURLConnection , and for a JAR JarURLConnection be returned.

Use this if you want to add some specific connection properties to your connection.

For instance:

 URLConnection urlConnection = google.openConnection(); urlConnection.setReadTimeout(1000); urlConnection.setConnectTimeout(1000); 
+2
source

Since the code for openStream() :

 public final InputStream openStream() throws java.io.IOException { return openConnection().getInputStream(); } 

It seems that it is really unnecessary.

But if I were you, if I openConnection() d, I would get InputStream in return URLConnection .

+1
source

openConnection() does not change the object of URL , it returns an instance of URLConnection , which you can then use. The code in question ignores the return value openConnection() , so in this case it is really pointless. it would be helpful if you are really doing something with that object compounds, for example, changing its timeout:

 URL google = new URL("http://www.google.com"); URLConnection conn = google.openConnection(); conn.setTimeout(7); // just an example BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); reader.lines().forEach(System.out::println); ))); URL google = new URL("http://www.google.com"); URLConnection conn = google.openConnection(); conn.setTimeout(7); // just an example BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); reader.lines().forEach(System.out::println); 
+1
source

All Articles