I would like to call a servlet from my servlet. I can call the remote servlet from a standalone application but I cannot call it from my servlet (it is on Glassfish). I use exactly the same code for the call (I get the error at the last code line):
URL serverAddress = new URL(endpoint);
//Set up the initial connection
HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setReadTimeout(timeOut);
connection.setRequestProperty("Content-Type", "text/xml; charset=ISO-8859-1");
connection.connect();
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(requestBody);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
It is suspicious that this code can't read the response of the remote servlet so probably the servlet doesn't reply at all. However why does it reply when I call it from standlone app? I really don't understand... I got this exception:
java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:769)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:766)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
Has anyone any idea? Is it possible that there are restricitions for servlets not to use the HttpURLConnection? Thanks!