2017-10-02 42 views
0

以下のコードを確認してください。コードが何をするかはfirstHttpConnを使ってレスポンスコードを確認し、secondHttpConnを開きます。応答コードが401の場合は、基本認証ヘッダーをsecondHttpConnに追加します。その後、データをポストして、応答を読み取ります。コードが「入力を読み取った後に出力を書き込めません」というエラーがなぜ発生するのですか?

ただし、コードは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 

誰もがなぜ知っていますか?ありがとう。

+0

@ luk2302私は前に 'connect()'を追加して同じ問題を投げたので問題にはならないと思います。 – NMSL

+0

ああ、私はあなたが2番目の接続を作成したことを見落としました。ごめんなさい。今私は頭の中の何が間違っているのか分かりません。おそらく、受け取ったスタックトレースを質問に追加するべきです。 – luk2302

+0

@ luk2302追加されました。ログには何もありません:( – NMSL

答えて

0

ここに貼り付けたコードは実行していません。あなたはHttpURLConnection.getOutputStreamは、スタックトレースにありますよう行は、一致していませんが、あなたはそのコードは、スタックトレースとあなたのペーストで異なり

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

を主張見ることができます。

ここに貼り付けたコードには問題はありません。

+0

私はコードが一致すると思います。コードを分離してローカルマシンで実行してみましょう。 – NMSL

+1

それは意見の問題ではなく、その行はgetOutputStream()を呼び出さないので一致しません。 – eis

+0

はい、あなたは正しいです。おじいちゃんのシステムは私の最新コードを手に入れませんでした。それを見つけ出すまでには時間がかかりました。 :) – NMSL

関連する問題