Ammad
Ammad

Reputation: 4235

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?

Upvotes: 0

Views: 258

Answers (2)

Harshit Thacker
Harshit Thacker

Reputation: 112

Hashcode could be same for both. Remember Hashcode and equals contract in javadocs.

please try to avoid relying on hashcode. As its not a good practice and in general cases hashcode are different each time. But its not assured to be unique.

You can use something like connection management api.

Please refer to below link. May be it can help.

If you can share your code or whole picture i can help in better way.

http://www.baeldung.com/httpclient-connection-management

Upvotes: 0

TeWu
TeWu

Reputation: 6564

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.

Upvotes: 2

Related Questions