title: Java problem with communication between HttpClient and HttpServer
body: <p>This is my first question here, so I apologize if not every information needed will be in this post.</p>
  <p>I'm having problem with communication between java.net.http.HttpClient and HttpServer.
  I send a request from application with HttpClient to application with Httpserver which the later reads, sends mail according to information in message and sends response to application with HttpClient, either succes if everything is OK or failure if something went wrong.</p>
  <p>First few times(it's random how many, sometimes 2 sometimes 24) are usually send correctly and handled correctly by Server which results in httpClient receiving response &quot;success&quot;, but suddenly Server gets request with correct header but without body, even though it is always the same one message in request, which results in &quot;failure&quot; response.</p>
  <p>My HttpClient:</p>
  <pre><code>    private Boolean sendMail(String requestBody) throws IOException, InterruptedException {
      HttpClient client = HttpClient.newBuilder()
              .version(HttpClient.Version.HTTP_1_1)
              .cookieHandler(CookieHandler.getDefault())
              .build();
  
      HttpRequest request = HttpRequest.newBuilder()
              .uri(URI.create(this.uri))
              .header(&quot;Content-Type&quot;, &quot;application/json&quot;)
              .POST(HttpRequest.BodyPublishers.ofString(requestBody))
              .build();
  
  
      //creating response body handler
      HttpResponse.BodyHandler&lt;String&gt; bodyHandler = HttpResponse.BodyHandlers.ofString();
  
  
      HttpResponse&lt;String&gt; response = client
              .send(request, bodyHandler);
  
      String requestOutcome = response.body();
      System.out.println(requestOutcome);
  </code></pre>
  <p>My HttpServer:</p>
  <pre><code>   HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
    ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
  
    server.createContext(&quot;/mial&quot;, new  ServerHttpHandler(this.dbHandler, this.mailHandler));
    server.setExecutor(threadPoolExecutor);
  
    //Start HttpServer
    server.start();
  </code></pre>
  <p>My custom ServerHttpHandler:</p>
  <pre><code> public void handle(HttpExchange httpExchange) throws IOException {
      String requestParamValue=null;
      if(&quot;GET&quot;.equals(httpExchange.getRequestMethod())) {
          requestParamValue = handleGetRequest(httpExchange);
      }else if(&quot;POST&quot;.equals(httpExchange.getRequestMethod())) {
          requestParamValue = handlePostRequest(httpExchange);
      }
      handleResponse(httpExchange,requestParamValue);
  }
  private String handleGetRequest(HttpExchange httpExchange) {
      return httpExchange.
              getRequestURI()
              .toString()
              .split(&quot;\\?&quot;)[1]
              .split(&quot;=&quot;)[1];
  }
  
  private String handlePostRequest(HttpExchange httpExchange){
      // get request
      Headers reqHeaders = httpExchange.getRequestHeaders();
      reqHeaders.forEach((key, value) -&gt; System.out.println(key + &quot;: &quot; + value));
  
      String message = null;
      try (InputStream in = httpExchange.getRequestBody()) {
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          StringBuilder msgbuilder = new StringBuilder();
          while (br.ready()) {
              msgbuilder.append((char) br.read());
          }
          message = msgbuilder.toString();
          System.out.println(&quot;Message: &quot; + message);
          br.close();
  
      } catch (IOException e) {
          e.printStackTrace();
      }
      return message;
  }
  
  private void handleResponse(HttpExchange httpExchange, String requestParamValue) throws IOException {
      boolean success = this.convertMsgAndSendMail(requestParamValue);
  
      String htmlResponse = &quot;failure&quot;;
      if(success) {
          htmlResponse = &quot;success&quot;;
      }
  
      httpExchange.sendResponseHeaders(200, htmlResponse.length());
      OutputStream outputStream = httpExchange.getResponseBody();
  
      outputStream.write(htmlResponse.getBytes());
      outputStream.flush();
      outputStream.close();
  }
  </code></pre>
  <p>Faulty request:</p>
  <pre><code>Connection: [Upgrade, HTTP1_1-Settings]
  Http2-settings: [AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA]
  Host: [localhost:587]
  Upgrade: [h2c]
  User-agent: [Java-http-client/11.0.6]
  Content-type: [application/json]
  Content-length: [180]
  Message:
  </code></pre>
  <p>So the header is the same every time, but this time I get no message. And as the message is later convert To JSON the only error i get is that message doesn't start with &quot;{&quot; so it isn't Json compatibile.</p>
  <p>I searched StackOverflow and other sites for solution to this problem but didn't found any useful tips how to resolve this. Do you have any idea?</p>