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

In code:

HttpURLConnection connection = (HttpURLConnection)createMerchURL.openConnection();
connection.setRequestMethod("PUT");
connection.addRequestProperty("Name", "Value1");

connection.connect();

..
connection.disconnect();
  • When does the connection really open? When ..createMerchURL.openConnection();? or in connection.connect();?

  • How to set url in object connectionand use it with connection.connect()? (since I'm less comfortable with .openConnection())

Finally, is there a difference between the two?

Thank..

+5
source share
3 answers

When does the connection really open? At ..createMerchURL.openConnection () ;? or when connecting .connect () ;?

Last. The first simply parses the URL, finds the protocol, and creates the object HttpURLConnection.

How to set a url in a connection object

You can not.

and use it with connection.connect ()?

You can not.

( .openConnection())

: .

+6

connection.connect() . , , , getContentLength(), getResponseCode(), , .

: Java

+3

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

+1
source

All Articles