Difference between URL.openConnection() and URLConnection.connect()?

断了今生、忘了曾经 提交于 2019-12-03 14:18:38

When is the connection getting opened actually? At ..createMerchURL.openConnection(); ? or at connection.connect();?

The latter. The former just parses the URL, finds the protocol, and creates the HttpURLConnection object.

How can I set URL in the connection Object

You can't.

and use it with connection.connect()?

You can't.

(as I am less comfortable with .openConnection())

Bad luck: get comfy with it.

Anirudha

connection.connect() is not required. Operations that depend on being connected, like getContentLength(), getResponseCode() will implicitly perform the connection if necessary.

Source: Java Docs

Joaquin Guevara

In addition to the other answers, if you just want to trigger some PHP File (via GET) in some address, you can simply use connect() after openConnection(), and then disconnect() of course.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!