Not possible, you could only send the header at the begening of the request, otherwise will be interpreted as part of the message. Relevant: https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
HTTP 1.1 is basically text over a connection, if you want to experiment open a socket in port 80 with the server and send text over the pipe, but all you get are probablly ERRO 400 Invalid Request.
Edit : For the sake of completation:
1- The client wich send headers and wait something before send data:
package requestholder;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class RequestHolder {
private static final DateFormat dateFormater= new SimpleDateFormat("yyyy-mm-aa hh:MM:ss");
private static final String USER_AGENT = "Mozilla/5.0";
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8084/WebApplication2/Hold");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
//no buffer, imediate flush
httpConnection.setChunkedStreamingMode(1);
//just in case....might matter for some servers
httpConnection.setAllowUserInteraction(true);
// Send post request
httpConnection.setDoOutput(true);
//add request headers
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("User-Agent", USER_AGENT);
httpConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
StringBuilder someDataToSend = new StringBuilder();
for (int i = 0; i < 10; i++) {
someDataToSend.append("word_:");
someDataToSend.append(i);
someDataToSend.append(" ");
}
DataOutputStream outPutWriter = new DataOutputStream(httpConnection.getOutputStream());
//if ther's somenthing on the pipe (wich i doubt..), flush
outPutWriter.flush();
//************ represents slow logic here (Wait for other Thread/Connection), the headers has been sent and makes server wait for data
Thread.sleep(20000);
// send the remaing data
outPutWriter.writeBytes(someDataToSend.toString());
outPutWriter.flush();
outPutWriter.close();
//read the response from here
int responseCode = httpConnection.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(httpConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
2- A simple servlet to prove the concept:
package teste;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*/
@WebServlet(name = "Hold", urlPatterns = {"/Hold"})
public class Hold extends HttpServlet {
private static final DateFormat format= new SimpleDateFormat("yyyy-mm-dd HH:MM:ss");
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//print the headers
Enumeration<String> headerNames = request.getHeaderNames();
String header;
while (headerNames.hasMoreElements()) {
header = headerNames.nextElement();
System.out.println(format.format(System.currentTimeMillis())+":"+ header + ":" + request.getHeader(header));
}
BufferedReader dataReader = request.getReader();
/* low on porpuse, to generate more lines*/
char data[] = new char[1500];
while(dataReader.read(data)>-1){
System.out.println(format.format(System.currentTimeMillis())+":"+ new String(data));
}
//write a standart response
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Hold</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Hold at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
3- The client output:
run:
Sending 'POST' request to URL : http://localhost:8084/WebApplication2/Hold
Response Code : 200
Servlet Hold
Servlet Hold at /WebApplication2
4- Finnally the servlet output, there's a 18 seconds ( How accurate is Thread.sleep? ) between headers and data:
2018-24-23 22:01:02:user-agent:Mozilla/5.0
2018-24-23 22:01:02:accept-language:en-US,en;q=0.5
2018-24-23 22:01:02:host:localhost:8084
2018-24-23 22:01:02:accept:text/html, image/gif, image/jpeg, *; q=.2, /; q=.2
2018-24-23 22:01:02:connection:keep-alive
2018-24-23 22:01:02:content-type:application/x-www-form-urlencoded
2018-24-23 22:01:02:transfer-encoding:chunked
2018-24-23 22:01:18:word_:0 word_:1 word_:2 word_:3 word_:4 word_:5 word_:6 word_:7 word_:8 word_:9
Credits:
https://www.mkyong.com/java/java-https-client-httpsurlconnection-example/
Implement pause/resume in file downloading
NetBeans templates.