这就是我遇到的问题。
/**
* Perform a post while retrieving an object in return.
* @param urlPath path to requested resource
* @param obj object to post
* @return object retrieved from server
*/
private Object doPostGet(String urlPath, Object obj) {
try {
URL url = new URL("http://" + this.host + ":" + this.port + urlPath);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "text/xml");
con.setRequestProperty("Accept", "text/plain");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
xStream.toXML(obj, con.getOutputStream());
con.getOutputStream().close();
Object outObj = null;
if (con.getResponseCode() == Handler.OK) {
InputStream in = new BufferedInputStream(con.getInputStream());
assert in != null;
outObj = xStream.fromXML(in);
in.close();
} else if (con.getResponseCode() == Handler.FAILED_AUTH) {
//
} else if (con.getResponseCode() == Handler.BAD_REQUEST) {
//
}
con.disconnect();
return outObj;
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:718)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:715)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at mock.communicator.Communicator.doPostGet(Communicator.java:131)
at mock.communicator.Communicator.main(Communicator.java:102)
Communicator的第131行是“if(con.getResponseCode()== Handler.OK){”。我不确定是什么导致它,我对这些东西有点新意,所以我的理解是有限的。如果你能帮助我理解我做错了什么就会很好。如果你需要任何服务器端代码让我知道,我正在使用HttpHandlers运行我自己的HttpServer。
答案 0 :(得分:-1)
Sotirios Delimanolis是对的。服务器没有发送响应头。在我的HttpHandler中,我只需要添加这段代码。 exchange.sendResponseHeaders(好的,0);
这解决了这个问题。非常感谢你们!