0

I am relatively new to Android. I have a very basic confusion in the functionality of 2 APIs which are used for network communications in Android.

  1. openConnection()
  2. connect()

From conn.connect() it appears that connect() is what actually creates the connection.

While from url.openConnection() it appears that openConnection() actually creates the connection.

From some discussions around the topic on SO, conn.connect() supports the former, with 4 upvotes for the answer while url.openConnection() supports the latter, with 97 upvotes for the answer.

Can someone please help clear the confusion ?

Community
  • 1
  • 1
qre0ct
  • 5,680
  • 10
  • 50
  • 86
  • https://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection it's surprisingly the 4 votes one. Url method really only creates a connection object which is used to do the connection. – zapl Dec 04 '15 at 14:06
  • @zapl Thanks for the pointer. The link you gave is informative. However, there is something confusing there as well. In one place it mentions that conn.getOutputStream(); does the network i/o and in the other it says that "// Unlike the identical call in the previous example, this // provokes no network IO. in = conn.getInputStream(); " Moreover, it does not mention anything about connect(). Would really appreciate some help on that as well. – qre0ct Dec 04 '15 at 14:23
  • 1
    `connect()` is automatically called by those methods when there is no connection yet. You don't have to call it yourself. Wrt unclear: The wording is a bit bad, what is meant is that `getInputstream` causes no request or such because http servers send the response immediately after they get the request all by themselves. Reading the response starts already with `getResponseCode`. – zapl Dec 04 '15 at 14:28
  • ok. That makes sense. Thanks. There is one more thing I would like to understand. Am working on an Xposed (framework for Android) module wherein I would like to hook all https network calls being made by an app to the backend server. What should be my approach ? To hook connect() (because now I understand that connect() will anyway be called by any of these methods) ? – qre0ct Dec 04 '15 at 14:34
  • I guess that would do it. Try it. (Lots of software also comes with their own http client implementation (ok http is quite popular) so you will not see all https call) – zapl Dec 04 '15 at 14:38
  • great. Thanks a lot. Your comments were very helpful (Y) – qre0ct Dec 04 '15 at 14:41

1 Answers1

2

So some further digging in actually helped me understand this a little more clearly.

As has been discussed in the comments above, and as is also stated here

  1. The connection object is created by invoking the openConnection method on a URL.
  2. The setup parameters and general request properties are manipulated.
  3. The actual connection to the remote object is made, using the connect method.
  4. The remote object becomes available. The header fields and the contents of the remote object can be accessed.

Now as far as conn.getInputStream() is concerned, as per this,

You are not always required to explicitly call the connect method to initiate the connection. Operations that depend on being connected, like getInputStream, getOutputStream, etc, will implicitly perform the connection, if necessary.

Now how exactly do they make the connection implicitly is something that I am still trying to figure out !

Community
  • 1
  • 1
qre0ct
  • 5,680
  • 10
  • 50
  • 86
  • Err, they call `connect()` if it hasn't been called yet. No mystery. – user207421 Dec 23 '15 at 11:39
  • @EJP Any supporting docs would be appreciated. Am writing an Xposed module to capture all HTTPS requests. Current approach - hook openConnection(), call connect() on the object. Requirement - hook actual connection itself. So hook connect() itself. Problem - getInputStream() used sometimes instead of connect(), & I can not hook two methods at the same time in Xposed. So, I wanted to know for sure that it is the connect() itself that gets called in case of getInputStream() as well. With this confirmation I can hook into connect() alone without really bothering about anything else. – qre0ct Dec 23 '15 at 18:22