Get name resolution time on HTTP request

Asked

Viewed 43 times

2

Long live,

Is it possible to obtain separate name resolution time from access time? The code I have is:

HttpURLConnection connection;
try {
    URL endereco = new URL(url);
    connection = (HttpURLConnection) endereco.openConnection();
    ...

it is possible to split openConnection in order to take times 1º of the resolution of the name to the DNS server and 2º the access time?

Thank you.

1 answer

3


You can use the InetAddress to make the name resolution before making the connection.

InetAddress inetAddress = InetAddress.getByName("pt.stackoverflow.com");

From this, you can connect to the address IP which has been solved, use the method getHostAddress to obtain the IP:

URL endereco = new URL("http", inetAddress.getHostAddress(), "/");

Note that when creating the URL I didn’t use the same constructor as your example, because only the IP does not provide all the information necessary to make the connection, so use a builder that meets the need.

  • 1

    +1 - perfect, if parsing well the name resolution is not part of HTTP itself

  • Thank you, another question, when connect, it tb will not make the name resolution incorporated? Making with this solution the resolution 2x?

  • 1

    Not in this case, because the inetAddress.getHostAddress() will return the host IP, and there will be no need for another name resolution.

  • my question was and when I asked: connection = (HttpURLConnection) endereco.openConnection();
 ...
 BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream()); I won’t be redoing the name resolution when connecting?

  • It will not, because you are using the IP to make the connection.

  • Thank you Henrique, reply accepted ;)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.