I have the following code:
URL urlServlet = new URL(WEB_SERVER_URL);
URLConnection connection = urlServlet.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setDefaultUseCaches(true);
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setRequestProperty("event", "blah");
OutputStream outputStream = servletConnection.getOutputStream();
outputStream.flush();
outputStream.close();
The server is not responding to this program.
But if I get inputStream from connection, I catch breakpoint in the DoGet servlet method.
What am I doing wrong?
Your mistake was to not asking for the response. The
URLConnectionis lazily executed. The request will only be sent whenever you ask for the response. CallinggetInputStream()will actually fire the HTTP request because you’re asking for the response. The connection will not be made when you just open and close the request body.See also: