调用的API是支持stream返回的,通过此方式可以流式获取返回结果。可以适当调整chunk size
public void getAnswer(@RequestBody JSONObject params) {
String url = wxyyService.wxyyUrl();
String userId = String.valueOf(SecurityUtils.getUserId());
try {
URL apiUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
connection.setChunkedStreamingMode(10);
connection.getOutputStream().write(params.toString().getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
myWebSocketUtils.sendOneMessage(userId, line);
}
reader.close();
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}