HttpURLConnection获取无效的XML响应

时间:2012-03-27 21:15:56

标签: java httpurlconnection

我们的Web服务代码正在尝试连接到Windows服务(返回XML响应),但我们有时会获得无效的XML响应异常。以下是我们在堆栈跟踪中看到的异常。

  

java.net.SocketTimeoutException:读取超时

     

SAXParseException:文件过早结束。

这是我们的遗留代码。除了缺少SOCKET连接时间外,还有其他任何建议。 readtimeout = 275000

try {
            URL u = new URL(targetURL);
            HttpURLConnection connection = (HttpURLConnection)u.openConnection();
            proertyName = Constant.READTIMEOUT;
            Integer timeOut = Integer.parseInt(dynamicLookup
                    .lookup(Constant.READTIMEOUT));
            connection.setReadTimeout(timeOut);

            String str = "&Id=" + xID;
            urlParameters += str;
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Length", Integer.valueOf(urlParameters.getBytes().length).toString());
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
            inputStream = connection.getInputStream();
        }
        catch (MalformedURLException exception) {


            log.error(ex.toString(), ex);
            throw ex;
        }
        catch (IOException exception) {


            log.error(ex.toString(), ex);
            throw ex;
        }
        catch (NumberFormatException exception) {

            log.error(ex);
            throw ex;
        }

阅读输入流

                 outputXMLString = IOUtils.toString(inputStream);
                 inputStream  = new ByteArrayInputStream(outputXMLString.getBytes());

                 return Handler.unMarshal(inputStream);//JAXB unmarshalling

0 个答案:

没有答案