简体   繁体   中英

Java IOException “Unexpected end of file from server” when posting large data

I am trying to post XML data to a server which processes this XML and gives a response back, some times the XML data can be very large and the server takes a while to process it, in those cases i fail to get any response back instead i receive a IOException with the message "Unexpected end of file from server". I am positive the XML being sent to the server is not full of errors, is there anything i can do on my end to make sure this doesn't happen or is this a server side issue? Below is code fragment of the method i call to post the data.

Thanks.

String encodedData="some XML"    
String urlString="example.com" 
try {
        URL url = new URL(urlString);
        HttpURLConnection urlConnection;
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", contentType);
        urlConnection.setRequestProperty("Content-Length", encodedData.length()+"");
            OutputStream os = urlConnection.getOutputStream();
            os.write(encodedData.getBytes());

        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
        StringBuffer sb = new StringBuffer();
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine);
        }
        in.close();
    } catch (MalformedURLException e) {
        logger.debug("MalformedURLException " + e.getMessage());
        logger.debug((new StringBuilder()).append("urlString=").append(urlString).toString());
        throw (e);
    } catch (IOException e) {
        logger.debug("IOException " + e.getMessage());
        logger.debug((new StringBuilder()).append("urlString=").append(urlString).toString());
        throw (e);
    }
return sb.toString();

客户端无法做很多事情,这只是服务器端的问题,其中服务器需要很长时间来处理数据,这导致服务器在发送响应之前连接超时。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM