Skip to content

Networking

For now... using a proxy

Proxy

Often needed at RINIS where everything has to go through a proxy. However java just plainly tries to connect to the given ip address at port 80. If you want a java program to use a proxy you have to specify that with flags :

proxy
-Dhttp.proxyHost="10.10.1.13" -Dhttp.proxyPort="3128"

Or.. use this programmatically. This example is for both http and https :

proxy
1
2
3
4
System.setProperty("http.proxyHost", "10.10.1.13");
System.setProperty("http.proxyPort", "3128");
System.setProperty("https.proxyHost", "10.10.1.13");
System.setProperty("https.proxyPort", "3128");

Network info

Related to the proxy setting, i wanted to detect on what network i was before setting a proxy (or not). I could aim for the hostname, but that would restrict me to one machine.

A very simple program that would list information like this (note that i added the hostname part myself)

output
Display name: utun0
Name: utun0
InetAddress: /fe80:0:0:0:493b:f345:139b:95f0%utun0

Display name: en0
Name: en0
InetAddress: /fe80:0:0:0:1064:4bb1:18a7:a935%en0
InetAddress: /10.10.3.145

Display name: lo0
Name: lo0
InetAddress: /fe80:0:0:0:0:0:0:1%lo0
InetAddress: /0:0:0:0:0:0:0:1
InetAddress: /127.0.0.1

cs-rinis-407.rinis.nl

Is this little java program ListNets.java :

network info
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class ListNets {

    public static void main(String args[]) throws SocketException {
        Enumeration[visit](NetworkInterface) nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
        out.printf("Display name: %sn", netint.getDisplayName());
        out.printf("Name: %sn", netint.getName());
        Enumeration[visit](InetAddress) inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            out.printf("InetAddress: %sn", inetAddress);
        }
        out.printf("n");
    }

    // and additional also a way to get the hostname :
    try {
        String hostname = InetAddress.getLocalHost().getHostName();
        out.println(hostname);
    } catch (Exception e) {
        out.println("Failed getting hostname");
    }
}  

Sauce: visit

From the InetAddress we could deduce that we are on the 10.10.3 network, meaning use a proxy.