When analysing network issues protocol quirks within the HTTP(s) protocoll you have to assemble custom requests. When utilizing web frameworks or http libraries often you do not have access to the byte stream at the level of socket connections.The following program provides an approach to send custom HTTP(s) requests to server applications.
The following application takes two arguments at command line. A hostname and a port number of the target system to connect to. The present example is porking with HTTP only. To work with HTTPS you have to initialize the encrypted connection, first.
/** * Source: www.Linux-Support.com * @author marioscondo */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.net.UnknownHostException; /** * Main class. */ public class Main { /** * @param arg1 hostname to connect to * @param arg2 port number to connect */ public static void main(String[] args) { String host = getParamAt(args, 0, "tools.linux-support.com"); int port = getParamAt(args, 1, 80); try { //Socket socket = new Socket("tools.linux-support.com", 80); Socket socket = new Socket(host, port); BufferedReader inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream outputStream = new PrintStream(socket.getOutputStream()); outputStream.println("POST /service-001/echo-01/hallo/req123?abc=a1bc3&client=test HTTP/1.1"); outputStream.println("Host: tools.linux-support.com"); outputStream.println("Accept: */*"); outputStream.println("Content-Length: 0"); //try to send an invalid header line //outputStream.println("Content-Type"); outputStream.println("Content-type: application/x-www-form-urlencoded"); outputStream.println(); outputStream.flush(); String line = null; while ((line =inputStream.readLine())!=null) { System.out.println(line); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** return a string from an array; with fallback value on error */ public static String getParamAt(String[] args, int pos, String defaultstr) { String ret = defaultstr; try { ret = args[pos]; } catch(Exception e) {} return ret; } /** return an integer from an array of strings; with fallback value on error */ public static Integer getParamAt(String[] args, int pos, Integer defaultint) { Integer ret = defaultint; try { ret = new Integer(args[pos]); } catch(Exception e) {} return ret; } }