代码之家  ›  专栏  ›  技术社区  ›  6324

为什么代码抛出“读取输入后无法写入输出”错误?

  •  -1
  • 6324  · 技术社区  · 8 年前

    firstHttpConn 检查响应代码并打开 secondHttpConn . 如果响应代码为401,则将基本身份验证标头添加到 第二个HTTPCONN . 然后发布数据并读取响应。

    Cannot write output after reading input 错误我检查了关于堆栈溢出的其他问题,如 this one 我确信我没有犯同样的错误。

    //I hate using Java 6 and HttpURLConnection, but the code is for a very old system.
    private static XmlObject callWebService(String soapMessage, String webServiceEndpoint, String soapAction) throws Exception {
        XmlObject resultXMLObject;
        HttpURLConnection firstHttpConn = null;
        HttpURLConnection secondHttpConn = null;
    
        try {
            byte[] streamoutByteArray = soapMessage.getBytes("UTF-8");
    
            URL url = new URL(webServiceEndpoint);
            firstHttpConn = (HttpURLConnection) url.openConnection();
            firstHttpConn.connect();
    
            if (firstHttpConn.getResponseCode() == 401) {
                firstHttpConn.disconnect();
                secondHttpConn = (HttpURLConnection) url.openConnection();
                secondHttpConn.setRequestProperty("Authorization", "Basic " + getBasicAuth());
            } else {
                firstHttpConn.disconnect();
                secondHttpConn = (HttpURLConnection) url.openConnection();
            }
    
            secondHttpConn.setConnectTimeout(30000);
            secondHttpConn.setReadTimeout(300000);
            secondHttpConn.setRequestProperty("Content-Length", String.valueOf(streamoutByteArray.length));
            secondHttpConn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8);
            secondHttpConn.setRequestProperty("SOAPAction", soapAction);
            secondHttpConn.setRequestMethod("POST");
            secondHttpConn.setDoOutput(true);
            secondHttpConn.setDoInput(true);
    
            OutputStream out = secondHttpConn.getOutputStream();
            out.write(streamoutByteArray);
            out.close();
    
            //Line 238 below.
            InputStreamReader isr = new InputStreamReader(secondHttpConn.getInputStream());
            BufferedReader in = new BufferedReader(isr);
            String inputLine;
            StringBuffer sb = new StringBuffer();
    
            while ((inputLine = in.readLine()) != null)
                sb.append(inputLine).append("\n");
    
            in.close();
            resultXMLObject = XmlObject.Factory.parse(sb.toString());
        } finally {
            if (firstHttpConn != null)
                firstHttpConn.disconnect();
            if (secondHttpConn != null)
                secondHttpConn.disconnect();
        }
    
        return resultXMLObject;
    }
    

    Caused By: java.net.ProtocolException: Cannot write output after reading input.
            at weblogic.net.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:271)
            at org.company.member.esb.webservice.WebServiceClient.callWebService(WebServiceClient.java:238)
            at org.company.member.esb.webservice.WebServiceClient.invokeWebserviceV01(WebServiceClient.java:132)
            at sun.reflect.GeneratedMethodAccessor711.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            Truncated. see log file for complete stacktrace
    

    1 回复  |  直到 8 年前
        1
  •  0
  •   eis    8 年前

    HttpURLConnection.getOutputStream 在堆栈跟踪中,但您声明

    //Line 238 below.
    InputStreamReader isr = new InputStreamReader(secondHttpConn.getInputStream());
    

    我看你贴在这里的代码没有问题。