Java HTTP Port Find executeHttpCommand(String host, int port, String request, String charset)

Here you can find the source of executeHttpCommand(String host, int port, String request, String charset)

Description

Connect to given host:port via direct TCP socket, send the input string, and return the result (if any) as a string

License

Open Source License

Declaration

public static String executeHttpCommand(String host, int port, String request, String charset)
        throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Objects;

public class Main {
    /**//  w w w. j  a  v  a 2s.com
     Connect to given host:port via direct TCP socket, send the input string, and
     return the result (if any) as a string
     */
    public static String executeHttpCommand(String host, int port, String request) throws Exception {
        return executeHttpCommand(host, port, request, "UTF-8");
    }

    /**
     Connect to given host:port via direct TCP socket, send the input string, and
     return the result (if any) as a string
     */
    public static String executeHttpCommand(String host, int port, String request, String charset)
            throws Exception {
        Objects.requireNonNull(host);
        Objects.requireNonNull(request);

        try (Socket socket = new Socket(host, port)) {
            socket.getOutputStream().write(request.getBytes(charset));
            socket.shutdownOutput();

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), charset));

            String response = "";
            String line = in.readLine();

            while (line != null) {
                response += line + "\n";
                line = in.readLine();
            }
            return response;
        }
    }
}

Related

  1. canConnect(String host, int port)
  2. canConnect(String host, int port)
  3. canConnectOn(String host, int port)
  4. createUnresolved(String host, int port)
  5. doSend(String command, String server, String port)
  6. exportResource(Class fromClass, String resourceName, String exportPath)
  7. findAvailablePort()
  8. findAvailablePort()
  9. findAvailablePort()