christo
christo

Reputation: 701

Can't send request from applet to servlet

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?

Upvotes: 0

Views: 767

Answers (1)

BalusC
BalusC

Reputation: 1108732

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:

Upvotes: 2

Related Questions