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 the exact same code to call (I get an error in the last line of code):
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()));
With the suspicion that this code cannot read the response of the remote servlet, the servlet may not be responding at all. However, why does it respond when I call it from a standalone application? 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)
Does anyone have any ideas? Is it possible for servlets there are restrictions on the use of HttpURLConnection? Thanks!
source share