Transfer-Encoding: chunked
API のエンドポイントからデータを取得しようとしていますが、HTTP POST メソッドで取得しようとしているデータが巨大で、API のサーバーがヘッダーの 1 つを次のように設定した応答を返すことに気付きました。データ全体を読み取ります。私のコードでは、を使用しjava.net.HttpURLConnection
て投稿リクエストを確立し、以下に示すようにデータを読み取ります。
残念ながら、このシナリオでは、 ( )java.io.IOException: Premature EOF
から読み取る行に到達し、デバッグしましたが、例外がスローされる直前までステータス http 200 を取得しています。BufferedReader
while((output = br.readLine()) != null)
コードに示されているようなチャンク データの要求に問題はありますか?
ありがとうございました。
String responseStr = "";
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(this.url).openConnection();
connection.setRequestProperty("content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
os.write(payloadToBeRequested.getBytes());
os.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String output = "";
while((output = br.readLine()) != null){
responseStr = responseStr + output;
}
status = connection.getResponseCode();
connection.disconnect();
}catch(MalformedURLException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
}