【发布时间】:2020-09-15 22:48:18
【问题描述】:
我正在使用 Java 调用 REST API,但有时响应有时会出现延迟。所以我想以某种方式检查响应是否花费太长时间(例如 2 或 3 秒)并重新启动/重新运行 http post 请求,因为我不需要“卡住”等待响应的请求,这可能来很多,很晚。
到目前为止,这是我的一段代码:
private final CloseableHttpClient client;
HttpResponse response;
List<NameValuePair> parameters;
HttpPost post;
public String getRoleData(int roleid) {
this.post = new HttpPost(this.createUrl("role/eventdata"));
this.post.setHeader("apikey", this.apikey);
this.parameters = new ArrayList<>();
this.parameters.add(new BasicNameValuePair("roleid", String.valueOf(roleid)));
try {
post.setEntity(new UrlEncodedFormEntity(this.parameters));
this.response = client.execute(post);
String JsonResponse = EntityUtils.toString(this.response.getEntity(), StandardCharsets.UTF_8);
return JsonResponse;
} catch (UnsupportedEncodingException ex) {
System.out.println("API Unsupported instruction entry");
} catch (IOException ex) {
System.out.println("API IO issue");
}
return "Empty response";
}
【问题讨论】:
标签: java api rest time response