@keinana1984

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

レスポンスデータの取得方法

通信中にHttpResponseのデータが欠けたとき、欠けた状態でいいのでその中身を見る方法を教えてください。
以下のソースを用意しています。(実際に動いているものを省略して貼り付けているので、細かいミスは無視してください。)

final HttpEntity entity = new StringEntity(json, "UTF-8");
httppost.setEntity(entity);

/* Post送信処理を行う */
httpResponse = httpClient.execute(httppost);

/* JSONデータ変換を行い、応答コードを取得する */
respEntity = httpResponse.getEntity();
ResponseBean ResponseBean = JSON.decode(
EntityUtils.toString(respEntity, strCharset), ResponseBean.class);
 
上記の処理中のEntityUtils.toStringでExceptionが発生したときに、以下の方法でレスポンスの中身を出力しようとしたのですが、writeToメソッドで、IOException:Attempted read from closed streamとなってしまいます。

if(respEntity != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
respEntity.writeTo(baos);
log.info("★writeTo:" + baos.toString());
  }

Entity.toStringをオーバーライドしてinputstreamをcloseしていた部分を、writeToより後のところでcloseしてみたのですがダメでした。

InputStream =new (BufferedHttpEntity)respEntity.getContent()も試しましたが、同じ結果でした。

何かいい方法はないでしょうか。よろしくお願いします。

0 likes

1Answer

Comments

  1. @keinana1984

    Questioner

    ご指摘ありがとうございます。
    修正する前に回答いただけましたので、次回以降気をつけます。

Your answer might help someone💌