In the code:
HttpURLConnection connection = (HttpURLConnection)createMerchURL.openConnection();
connection.setRequestMethod("PUT");
connection.addRequestProperty("Name", "Value1");
connection.connect();
..
connection.disconnect();
When is the connection getting opened actually? At
..createMerchURL.openConnection();
? or atconnection.connect();
?How can I set URL in the
connection
Object and use it withconnection.connect()
?(as I am less comfortable with.openConnection()
)
Finally, is there any difference between the two?
Thanks..
connection.connect()
is not required. Operations that depend on being connected, likegetContentLength(), getResponseCode()
will implicitly perform the connection if necessary.Source: Java Docs
The latter. The former just parses the URL, finds the protocol, and creates the
HttpURLConnection
object.You can't.
You can't.
Bad luck: get comfy with it.
In addition to the other answers, if you just want to trigger some PHP File (via
GET
) in some address, you can simply useconnect()
afteropenConnection()
, and thendisconnect()
of course.