Java Utililty Methods HTTP Port Find

List of utility methods to do HTTP Port Find

Description

The list of methods to do HTTP Port Find are organized into topic(s).

Method

intgetRandomPort()
get Random Port
try (ServerSocket s = new ServerSocket(0)) {
    return s.getLocalPort();
} catch (IOException e) {
    throw new RuntimeException("Can't get random port");
ListgetRandomPorts(int n)
Create a list of currently available port numbers as strings.
List<String> ports = new ArrayList<>();
for (int i = 0; i < n; i++) {
    ports.add(String.valueOf(getRandomPort()));
return ports;
StringgetResponse(String name, String host, int port)
get Response
URL url = new URL("http://" + host + ":" + port + "/" + name);
HttpURLConnection connection = connect(url);
return toString(new BufferedInputStream(connection.getInputStream()));
intgetServerPort()
Get a server socket point for server, either TCP or UDP.
int serverPort = 0;
try {
    ServerSocket serverSocket = new ServerSocket(0);
    serverPort = serverSocket.getLocalPort();
    serverSocket.close();
} catch (IOException e) {
    throw new RuntimeException("Failed to get a server socket point");
return serverPort;
String[]getSupportedConnectionTypes(Map map)
get Supported Connection Types
String[] types = null;
Object[] typeObjs = (Object[]) map.get("supportedConnectionTypes");
if (typeObjs != null) {
    types = new String[typeObjs.length];
    for (int i = 0; i < typeObjs.length; i++) {
        types[i] = (String) typeObjs[i];
return types;
SetgetSupportedEntityTypes()
Returns a Set of supported entity types
return supportedEntities;
intgetUnusedPort()
Get a port that isn't currently used.
ServerSocket s = new ServerSocket(0);
int port = s.getLocalPort();
s.close();
return port;
intgetUnusedPort(final int start)
get Unused Port
return getUnusedPort(start, DEFAULT_PORT_RANGE_END);
intgetUnusedPort(int startPort)
Returns an unused port number on the local host.
final int nTry = 20;
for (int iPort = startPort; iPort < startPort + nTry; iPort++) {
    try {
        Socket trySocket = new Socket("localhost", iPort);
        if (!trySocket.isClosed()) {
            if (WARN_ABOUT_NOSUCHELEMENTEXCEPTIONS) {
                WARN_ABOUT_NOSUCHELEMENTEXCEPTIONS = false;
                System.err.println(
...
int[]getUnusedPorts(final int count, final int start)
get Unused Ports
return getUnusedPorts(count, start, DEFAULT_PORT_RANGE_END);