我想从我的 servlet 调用一个 servlet。我可以从独立应用程序调用远程 servlet,但不能从我的 servlet 调用它(它在 Glassfish 上)。我使用完全相同的代码进行调用(我在最后一行代码中得到错误):
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()));
怀疑此代码无法读取远程 servlet 的响应,因此 servlet 可能根本没有回复。但是,为什么当我从独立应用程序调用它时它会回复?我真的不明白......我得到了这个例外:
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)
有人知道吗?servlet 是否有可能限制不使用 HttpURLConnection?谢谢!