title: HttpURLConnection : Why is there 5 seconds delay with every chunk when Expect:100-Continue Header is specified?
body: <p>I am experimenting with <a href="https://datatracker.ietf.org/doc/html/rfc7231#section-5.1.1" rel="nofollow noreferrer">RFC Expect</a> Header in java <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/HttpURLConnection.html?msclkid=a043c46ca54f11ecaff177aff4f8c62f" rel="nofollow noreferrer">HttpURLConnection</a> which works perfectly except for one detail.</p>
  <p>There is an 5 second wait period between sending the body of an <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/HttpURLConnection.html?msclkid=a043c46ca54f11ecaff177aff4f8c62f#setFixedLengthStreamingMode(int)" rel="nofollow noreferrer">Fixed Length Mode</a> or between each chunk in <a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/HttpURLConnection.html?msclkid=a043c46ca54f11ecaff177aff4f8c62f#setChunkedStreamingMode(int)" rel="nofollow noreferrer">Chunk Streaming Mode</a></p>
  <p>Here is the client class</p>
  <pre><code>public static void main(String[] args)throws Exception
   {
     HttpURLConnection con=(HttpURLConnection)new URL(&quot;http://192.168.1.2:2000/ActionG&quot;).openConnection();
     
     //for 100-Continue logic
     con.setRequestMethod(&quot;POST&quot;);
     con.setRequestProperty(&quot;Expect&quot;, &quot;100-Continue&quot;);
    
     //responds to 100-continue logic
     con.setDoOutput(true);
     con.setChunkedStreamingMode(5);
     con.getOutputStream().write(&quot;Hello&quot;.getBytes());
     con.getOutputStream().flush();
     con.getOutputStream().write(&quot;World&quot;.getBytes());
     con.getOutputStream().flush();
     con.getOutputStream().write(&quot;123&quot;.getBytes());
     con.getOutputStream().flush();
  
     //decode response and response body/error if any
     System.out.println(con.getResponseCode()+&quot;/&quot;+con.getResponseMessage());      
     con.getHeaderFields().forEach((key,values)-&gt;
     {
      System.out.println(key+&quot;=&quot;+values);
      System.out.println(&quot;====================&quot;);
     });      
     try(InputStream is=con.getInputStream()){System.out.println(new String(is.readAllBytes()));}
     catch(Exception ex)
     {
      ex.printStackTrace(System.err);
     
      InputStream err=con.getErrorStream();
      if(err!=null)
      {
       try(err){System.err.println(new String(is.readAllBytes()));}
       catch(Exception ex2){throw ex2;}
      }  
     } 
     con.disconnect();
    }
  </code></pre>
  <p>I am uploading 3 chunks. In the server side 5 packets of data are received</p>
  <ol>
  <li><p>All headers. respond with 100 Continue</p>
  </li>
  <li><p>3 Chunks. For each chunk respond with 100 Continue</p>
  </li>
  <li><p>Last Chunk[Length 0]. respond with 200 OK</p>
  </li>
  </ol>
  <p>Here is the Test Server</p>
  <pre><code>final class TestServer
  {
   public static void main(String[] args)throws Exception
   {
    try(ServerSocket socket=new ServerSocket(2000,0,InetAddress.getLocalHost()))
    {
     int count=0;
     try(Socket client=socket.accept())
     {
      int length;
      byte[] buffer=new byte[5000];
      InputStream is=client.getInputStream();
      
      OutputStream os=client.getOutputStream();
      while((length=is.read(buffer))!=-1)
      {
       System.out.println(++count);
       System.out.println(new String(buffer,0,length));
       System.out.println(&quot;==========&quot;);
    
       if(count&lt;5)
       {
        os.write(&quot;HTTP/1.1 100 Continue\r\n\r\n&quot;.getBytes());
        os.flush();
       }
       else
       {
        os.write(&quot;HTTP/1.1 200 Done\r\nContent-Length:0\r\n\r\n&quot;.getBytes());
        os.flush();
        break;
       }   
      }
     }
    }
   }
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>1
  POST /ActionG HTTP/1.1
  Expect: 100-Continue
  User-Agent: Java/17.0.2
  Host: 192.168.1.2:2000
  Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  Connection: keep-alive
  Content-type: application/x-www-form-urlencoded
  Transfer-Encoding: chunked
  
  
  ==========   //5 seconds later
  2
  5
  Hello
  
  ==========   //5 seconds later
  3
  5
  World
  
  ==========  //5 seconds later
  //this and the last chunk come seperatly but with no delay
  4
  3
  123
  
  ==========
  5
  0
  
  
  ==========
  </code></pre>
  <p>I have checked every Timeout Method in my Connection Object</p>
  <pre><code>System.out.println(con.getConnectTimeout());
  System.out.println(con.getReadTimeout());
  </code></pre>
  <p>Both return 0</p>
  <p>So where is this 5 second delay coming from?</p>
  <p>I am using jdk 17.0.2 with windows 10</p>