【问题标题】:HttpUrlConnection getResponseCode throwing java.net.SocketException: Unexpected end of file from server [duplicate]HttpUrlConnection getResponseCode抛出java.net.SocketException:来自服务器的文件意外结束[重复]
【发布时间】:2013-08-30 17:10:38
【问题描述】:

这就是我遇到的问题。

    /**
 * 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。

【问题讨论】:

  • 这是服务器端问题。它是如何响应的?
  • 你是对的,服务器正在崩溃,但它没有抛出任何奇怪的异常。我认为这可能与发生的双重 POST 有关。每当我做一个 POST 时,它会出于某种原因发送 2 次,而不是只发送一次。你能看到任何证据表明是什么原因造成的吗?
  • 并非如此。逐行调试并检查。
  • Sotirios Delimanolis 是正确的。服务器未发送响应标头。在我的 HttpHandler 中,我只需要添加这段代码。 exchange.sendResponseHeaders(OK, 0);这解决了这个问题。非常感谢你们!

标签: java httpurlconnection httpserver


【解决方案1】:

Sotirios Delimanolis 是正确的。服务器未发送响应标头。在我的 HttpHandler 中,我只需要添加这段代码。 exchange.sendResponseHeaders(OK, 0);

这就解决了这个问题。非常感谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    相关资源
    最近更新 更多