Search code examples
javahttpsurlconnection

How to get unique ID of HttpsUrlConnection Object


I am using HttpsURLConnection in Java and through some sort of loop i am creating 10 connections which later on used by different threads inside my program.

HttpsURLConnection connection = null;
connection =    (HttpsURLConnection) url.openConnection();

Wondering how can i know which connection is used by current thread. I Just need some unique identifier of the connection.

Some thing like,

System.out.println(connection);

But above System.out statement is printing pretty generic server name. Not the hash code of connection object.

Wondering how can i do that?


Solution

  • The hashCode method is defined on java.lang.Object (and overridden in subclasses when needed), so you can call it on any object. In your case connection.hashCode() should give you the hash code of connection object.