我在URLConnection
类中有一个难以理解connect()
方法的含义。在下面的代码中,如果我使用connect()
方法,如果我不使用它,我会得到相同的结果。Java URLConnection - 什么时候需要使用connect()方法?
为什么(或何时)需要使用它?
URL u = new URL("http://example.com");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.connect();//with or without it I have the same result
InputStream in = conn.getInputStream();
int b;
while ((b = in.read()) != -1) {
System.out.write(b);
}
完全正确的调用。 Downvoter请解释。 +1 – EJP 2014-05-14 01:16:00