Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HttpUrlConnection getResponseCode引发java.net.SocketException:服务器的文件意外结束_Java_Httpurlconnection_Httpserver - Fatal编程技术网

HttpUrlConnection getResponseCode引发java.net.SocketException:服务器的文件意外结束

HttpUrlConnection getResponseCode引发java.net.SocketException:服务器的文件意外结束,java,httpurlconnection,httpserver,Java,Httpurlconnection,Httpserver,这就是我遇到的情况 /** * 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) {

这就是我遇到的情况

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

通信器的第131行是“如果(con.getResponseCode()==Handler.OK){”.我不确定是什么原因造成的,我对这方面的知识还不太熟悉,所以我的理解有限。如果你能帮助我理解我做错了什么,那就太好了。如果你需要任何服务器端代码,请告诉我,我正在使用HttpHandler运行我自己的HttpServer。

Sotirios Delimanolis是正确的。服务器没有发送在我的HttpHandler中,我只需要添加这段代码。 exchange.sendResponseHeaders(确定,0)


这就解决了问题。非常感谢大家!Sotirios Delimanolis是正确的。服务器没有发送响应头。在我的HttpHandler中,我只需要添加这段代码。 exchange.sendResponseHeaders(确定,0)


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

这是服务器端的问题。它是如何响应的?你是对的,服务器正在崩溃,但它没有抛出任何异常。我认为这可能与正在发生的双重帖子有关。每当我发帖子时,它都会出于某种原因发送两次,而不仅仅是once。你能看到导致这种情况的任何证据吗?不是真的。逐行调试并检查。Sotirios Delimanolis是正确的。服务器没有发送响应头。在我的HttpHandler中,我只需要添加这段代码。exchange.sendResponseHeaders(确定,0);这就解决了问题。非常感谢各位!这是一个服务器端问题。它如何响应?你是对的,服务器正在崩溃,但它没有抛出任何异常。我认为这可能与正在发生的双重帖子有关。每当我发帖子时,它都会出于某种原因发送两次,而不是一次。你能看到导致这种情况的任何证据吗?不是真的。逐行调试并检查。Sotirios Delimanolis是正确的。服务器没有发送响应头。在我的HttpHandler中,我只需要添加这段代码。exchange.sendResponseHeaders(确定,0);这就解决了问题。非常感谢大家!