[英]Is it really necessary to use url.openConnection()?
As we all know both these codes will yield 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);
}
}
So what's the point in using google.openConnection()?
那么使用
google.openConnection()?
May be javadoc for this method helps: 可能是这个方法的javadoc有帮助:
public java.net.URLConnection openConnection() throws java.io.IOException
Returns a
URLConnection
instance that represents a connection to the remote object referred to by theURL
.返回一个
URLConnection
实例,该实例表示与URL
引用的远程对象的连接。 A new instance ofURLConnection
is created every time when invoking theURLStreamHandler.openConnection(URL)
method of the protocol handler for this URL.每次调用此URL的协议处理程序的
URLStreamHandler.openConnection(URL)
方法时,都会创建一个新的URLConnection
实例。It should be noted that a
URLConnection
instance does not establish the actual network connection on creation.应该注意,
URLConnection
实例在创建时不建立实际的网络连接。 This will happen only when callingURLConnection.connect()
.只有在调用
URLConnection.connect()
时才会发生这种情况。If for the
URL
's protocol (such as HTTP or JAR), there exists a public, specializedURLConnection
subclass belonging to one of the following packages or one of their subpackages:java.lang
,java.io
,java.util
,java.net
, the connection returned will be of that subclass.如果对于
URL
的协议(例如HTTP或JAR),存在属于以下包之一或其子包之一的公共专用URLConnection
子类:java.lang
,java.io
,java.util
,java.net
,返回的连接将是该子类。 For example, for HTTP anHttpURLConnection
will be returned, and for JAR aJarURLConnection
will be returned.例如,对于HTTP,将返回
HttpURLConnection
,对于JAR,将返回JarURLConnection
。
Use this if you want to add some specific connectivity properties to your connection. 如果要为连接添加一些特定的连接属性,请使用此选项。
For example: 例如:
URLConnection urlConnection = google.openConnection();
urlConnection.setReadTimeout(1000);
urlConnection.setConnectTimeout(1000);
Since the code for openStream()
is: 由于
openStream()
的代码是:
public final InputStream openStream() throws java.io.IOException {
return openConnection().getInputStream();
}
It seems quite redundant indeed. 这看起来确实很多余。
But if I were you, if I openConnection()
d, I would then get the InputStream
on the returned URLConnection
. 但如果我是你,如果我是
openConnection()
d,那么我会在返回的URLConnection
上获取InputStream
。
openConnection()
does not modify the URL
object, it returns a URLConnection
instance that you could then use. openConnection()
不修改URL
对象,它返回一个URLConnection
情况下,你可以再使用。 The code in the question ignores the return value of openConnection()
, so, in this case, it's indeed pointless. 问题中的代码忽略了
openConnection()
的返回值,因此,在这种情况下,它确实毫无意义。 it would only be useful if you actually do something with this connection object, such as, eg, modifying 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.