小编kap*_*ppa的帖子

Java URLConnection - 我什么时候需要使用connect()方法?

我有一个问题是要理解该类中connect()方法的含义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);
}
Run Code Online (Sandbox Code Playgroud)

java urlconnection connect httpurlconnection

32
推荐指数
2
解决办法
2万
查看次数

标签 统计

connect ×1

httpurlconnection ×1

java ×1

urlconnection ×1