java-networking

URLs and URIs

The Real Difference : URIs vs URLs vs URNs

All URLs are URIs, but not all URIs are URLs.

URI Types and Subtypes

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.

Structure of a URL

URL structure

The URL Class

Creating new URL objects

@returns URL object

@throws MalformedURLException if the URL is not properly formatted

URL url = new URL("http://www.example.com/index.html");

Retrieving Data from a URL

@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());

Getter Methods :

The URI Class

Creating new URI objects

@returns URI object

@throws URISyntaxException if the URI is not properly formatted

URI uri = new URI("http://www.example.com/index.html");

Getter Methods :

Resolving Relative URIs

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);

The URLEncoder and URLDecoder Classes

@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.

URLEncoder

public static String encode(String s, String enc)

Encodes a string into application/x-www-form-urlencoded format using a specific encoding scheme.

URLDecoder

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);

Proxies

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.

Working of Proxy Server

System Properties

System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");

The Proxy Class

The Proxy class represents a proxy setting, consisting of a proxy type and an address.

public Proxy(Proxy.Type type, SocketAddress sa)

Types :

SocketAddress address = new InetSocketAddress("proxy.example.com", 8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);

The ProxySelector Class

ProxySelector is an abstract class that determines which proxy (if any) to use for a given network connection.

Key Points:

Main Methods:

Communicating with server side programs through GET

Accessing Password Protected Sites

The authenticator class

The Password authentication class

Jpassword field class