gpt4 book ai didi

java - 如何设置InputStreamReader的超时时间?

转载 作者:行者123 更新时间:2023-12-02 04:29:37 25 4
gpt4 key购买 nike

所以我目前用 Java 执行 HTTP 请求:

HttpURLConnection connection = (HttpURLConnection) apiURL.openConnection();

connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();

JSONObject json = (JSONObject) new JSONParser().parse(new InputStreamReader(connection.getInputStream()));
connection.disconnect();

我已经看到很多关于套接字的提及,但我不确定它在这里如何应用。我如何将此 HTTP 请求的超时设置为 1 秒?

最佳答案

您可以设置 HTTP 连接超时,例如:-

 connection.setConnectTimeout(5000); // 5 seconds connectTimeout
connection.setReadTimeout(5000 ); // 5 seconds socketTimeout

关于java - 如何设置InputStreamReader的超时时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31640174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com