我有一个问题,不理解URLConnection
类中的connect()
方法的含义。在下面的代码中,如果我使用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);
}