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 (String url)
- URL (String protocol, String host, String path)
- URL (String protocol, String host, int port, String path)
- URL (URL baseURL, String relativeURL)
URL url = new URL("http://www.example.com/index.html");
@throws
IOException
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)
}
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);
}
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 (String uri)
- URI (String scheme, String schemaSpecificPart, String fragment)
- URI (String scheme, String authority, String path, String query, String fragment)
- URI (String scheme, String host, String path, String fragment)
URI uri = new URI("http://www.example.com/index.html");
-
public String getScheme()
: Returns the scheme of the URI -
public String getAuthority()
: Returns the authority of the URI -
public String getUserInfo()
: Returns the user info of the URI -
public String getHost()
: Returns the host name of the URI -
public int getPort()
: Returns the port number of the URI -
public String getPath()
: Returns the path of the URI -
public String getQuery()
: Returns the query string of the URI -
public String getFragment()
: Returns the fragment of the URI -
getRaw<url-part>()
: Returns the corresponding info of the url part
public 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.
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);
- Always specify the character encoding (e.g., UTF-8) when using these methods to ensure consistent behavior across different platforms.
- The
encode()
method replaces spaces with '+' and encodes special characters as hex values preceded by '%'. - *The
decode()
method reverses this process, converting '+' back to spaces and '%XX' hex values back to their original characters. - These classes are particularly useful when working with query parameters in URLs or when handling user input that will be part of a URL.
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 proxy -
Proxy.Type.SOCKS
: Represents a SOCKS (V4 or V5) proxy
SocketAddress 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.
- Allows dynamic proxy selection based on the URI of the connection
- Can be used to implement custom proxy selection logic
- Default selector can be set system-wide
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