Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid, that URL.equals needs access to the internet in Java?

The equals() method of the URL class in the Java class library makes a DNS request to get the IP for the hostname, to check the two IP's for equality. This happens even for URLs that are created from the same String. Is there a way to avoid this internet access?

like image 810
Mnementh Avatar asked Nov 13 '08 00:11

Mnementh


People also ask

How do you represent a URL in Java?

In your Java program, you can use a String containing this text to create a URL object: URL myURL = new URL("http://example.com/"); The URL object created above represents an absolute URL. An absolute URL contains all of the information necessary to reach the resource in question.

What is openStream () in Java?

The openStream() method returns a java.io.InputStream object so you can read from a URL using the normal InputStream methods. Input and Output Streams. describes the I/O classes provided by the Java development environment and shows you how to use them. Reading from a URL is as easy as reading from an input stream.

What does URL openConnection do?

openConnection. Returns a URLConnection instance that represents a connection to the remote object referred to by the URL . A new instance of URLConnection is created every time when invoking the URLStreamHandler. openConnection(URL) method of the protocol handler for this URL.

What is URL in Java networking?

URL known as Uniform Resource Locator is simply a string of text that identifies all the resources on the Internet, telling us the address of the resource, how to communicate with it, and retrieve something from it.


2 Answers

Use java.net.URI instead of URL.

like image 186
Bill the Lizard Avatar answered Nov 11 '22 15:11

Bill the Lizard


If you just want to compare the url strings, try

url1.toString().equals(url2.toString())
like image 41
Rick Avatar answered Nov 11 '22 13:11

Rick