0

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?

christo
  • 671
  • 1
  • 8
  • 26
  • [this link](http://www2.sys-con.com/itsg/virtualcd/java/archives/0309/darby/index.html) may help you – harry Feb 23 '12 at 10:18
  • At a guess, you probably want to set the method to something other than `GET`. `PUT` by the looks of it. And similarly for the servlet, don't use `doGet`. – Tom Hawtin - tackline Feb 23 '12 at 10:21

1 Answers1

2

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 URLConnection is lazily executed. The request will only be sent whenever you ask for the response. Calling getInputStream() 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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555