All URLs are URIs, but not all URIs are URLs.
A Uniform Resource Identifier (URI)
is a string of characters that uniquely identify a name or a resource on the internet. A URI identifies a resource by name, location, or both. URIs have two specializations known as Uniform Resource Locator (URL), and Uniform Resource Name (URN).
A Uniform Resource Locator (URL)
is a type of URI that specifies not only a resource, but how to reach it on the internet—like http://, ftp://, or mailto://.
A Uniform Resource Name (URN)
is a type of URI that uses the specific naming scheme of urn:—like urn:isbn:0-486-27557-4 or urn:isbn:0-395-36341-1.
So a URI or URN is like your name, and a URL is a specific subtype of URI that’s like your name combined with your address.
The Scheme
, which is the protocol that you’re using to interact. e.g. HTTP, HTTPS, FTP, MAILTO, IRC, FILE
The Authority
, which is the target you’re accessing. This breaks down into userinfo, host, and port.
The Path
, which is the resource you’re requesting on the host.
The Query
, which are the parameters being used within the web application.
The Fragment
, which is the target to jump to within a given page.
@returns
URL
object
@throws
MalformedURLException
if the URL is not properly formatted
URL url = new URL("http://www.example.com/index.html");
@throws
IOException
InputStream openStream()
connects to the resource referenced by the URL, performs any necessary handshaking between client and the server, and re
try {
String location = "http://lolcats.com/";
URL url = new URL(location);
InputStream is = url.openStream();
BufferReader br = new BufferReader(new InputStreamReader(is));
String line;
while((line = br.readLine())!= null) {
System.out.println(line);
}
} catch(IOException ex) {
System.out.println(ex)
}
URLConnection openConnection()
opens a socket to the specified URL and returns a URLConnection
object
URL url = new URL(location);
URLConnectioin conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(is));
String line;
while((line = br.readLine())!= null) {
System.out.println(line);
}
URL Connection openConnection(Proxy proxy)
Object getContent()
retrieves the data referenced by the URL and tried to make it into some type of object
URL url = new URL(location);
Object content = url.getContent();
sout(content.getClass().getName());
public String getProtocal()
: Returns the protocol of the URLpublic String getHost()
: Returns the host name of the URLpublic int getPort()
: Returns the port number of the URLpublic String getPath()
: Returns the path of the URLpublic String getQuery()
: Returns the query string of the URLpublic String getRef()
: Returns the reference of the URLpublic String getFile()
: Returns the file name of the URLpublic String getAuthority()
: Returns the authority of the URLpublic String getDefaultPort()
: Returns the default port of the URLpublic String getUserInfo()
: Returns the user info of the URL@returns
URI
object
@throws
URISyntaxException
if the URI is not properly formatted
URI uri = new URI("http://www.example.com/index.html");
public String getScheme()
: Returns the scheme of the URIpublic String getAuthority()
: Returns the authority of the URIpublic String getUserInfo()
: Returns the user info of the URIpublic String getHost()
: Returns the host name of the URIpublic int getPort()
: Returns the port number of the URIpublic String getPath()
: Returns the path of the URIpublic String getQuery()
: Returns the query string of the URIpublic String getFragment()
: Returns the fragment of the URI
getRaw<url-part>()
: Returns the corresponding info of the url partpublic URI resolve(URI uri)
: Resolves the given URI against this URI.URI base = new URI("http://www.example.com/index.html");
URI relative = new URI("/about.html");
URI resolved = base.resolve(relative);
System.out.println(resolved);
@returns
String
object
@throws
UnsupportedEncodingException
if the encoding is not supported
These utility classes are used to encode and decode strings for use in URLs, ensuring that special characters are properly handled.
public static String encode(String s, String enc)
Encodes a string into application/x-www-form-urlencoded format using a specific encoding scheme.
public static String decode(String s, String enc)
Decodes URL-encoded strings back into their original form.
String url = "http://www.example.com/index.html?name=John Doe";
String encodedURL = URLEncoder.encode(url, "UTF-8");
System.out.println(encodedURL);
// Output : http%3A%2F%2Fwww.example.com%2Findex.html%3Fname%3DJohn+Doe
String decodedURL = URLDecoder.decode(encodedURL, "UTF-8");
System.out.println(decodedURL);
encode()
method replaces spaces with ‘+’ and encodes special characters as hex values preceded by ‘%’.decode()
method reverses this process, converting ‘+’ back to spaces and *‘%XX’ hex values back to their original characters.A proxy server is an intermediary server that sits between the client and the server. It intercepts requests from clients and forwards them to the server, then forwards the server’s response back to the client. Proxies are commonly used to filter requests, cache data, or provide anonymity for clients.
System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");
The Proxy
class represents a proxy setting, consisting of a proxy type and an address.
public Proxy(Proxy.Type type, SocketAddress sa)
Types :
Proxy.Type.DIRECT
: Represents a direct connection, or the absence of a proxy
Proxy.Type.HTTP
: Represents an HTTP proxyProxy.Type.SOCKS
: Represents a SOCKS (V4 or V5) proxySocketAddress address = new InetSocketAddress("proxy.example.com", 8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
ProxySelector
is an abstract class that determines which proxy (if any) to use for a given network connection.
static ProxySelector getDefault()
: Gets the current default selectorstatic void setDefault(ProxySelector ps)
: Sets the default selectorabstract List<Proxy> select(URI uri)
: Selects proxies for a given URIabstract void connectFailed(URI uri, SocketAddress sa, IOException ioe)
: Called when a connection fails