0

I have written code to connect to a to a URL using a HttpURLConnection.

            String str_URL = "http://localhost:8886/bod-sep-service/a/b/c";
            java.net.URL url = new java.net.URL(str_URL);
            
            java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(6000);
            connection.setReadTimeout(6000);

            int responseCode = connection.getResponseCode();
            
            if (responseCode == java.net.HttpURLConnection.HTTP_OK) {
                // do Something
            }

I do not have access to the URL so I am trying to simulate it and send an answer to my code from above:

Thread thread = new Thread() {
           public void run() {
               try {
                   OutputStream outputStream;
                   String boundary = "===" + System.currentTimeMillis() + "===";
                   java.net.URL url = new java.net.URL("http://localhost:8886/bod-sep-service/a/b/c");
                   java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
                   connection.setRequestMethod("POST");

                   connection.setUseCaches(false);
                   connection.setDoOutput(true); // indicates POST method
                   connection.setDoInput(true);
                   connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
                   while(true) {
                       outputStream = connection.getOutputStream();
                       // send answer?
                   }
               } catch (MalformedURLException e) {
                   System.out.println(e.getMessage());
               } catch (IOException e) {
                   System.out.println(e.getMessage());
               }

           }
       };
       thread.start();

I did not find any examples online on how to simulate the URL and how to send an answer. Any help will be much appreciated.

schmimona
  • 849
  • 3
  • 20
  • 40
  • you can use netcat, see examples - https://stackoverflow.com/questions/16640054/minimal-web-server-using-netcat – ursa Apr 11 '23 at 08:43

0 Answers0