如果响应时间在 Java 中超过 n 秒,则重新运行 http post 请求?
Rerun http post request if response time is longer than n seconds in Java?
我正在用 Java 调用 REST API,但响应有时会偶尔出现延迟。所以我想以某种方式检查响应是否花费了太长时间(例如 2 或 3 秒)和 restart/rerun 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";
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class Test {
public static void main(String[] args) {
int timeoutInMillis = 10;
RequestConfig config = RequestConfig.custom().
setConnectTimeout(timeoutInMillis).
setConnectionRequestTimeout(timeoutInMillis).
setSocketTimeout(timeoutInMillis).build();
final CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(config).build();
HttpPost post = new HttpPost("https://reqres.in/api/api/users");
//this.post.setHeader("apikey", this.apikey);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new NameValuePair("name", "test1"));
parameters.add(new NameValuePair("job", "job1"));
try {
//post.setEntity(new UrlEncodedFormEntity(this.parameters));
HttpResponse response = client.execute(post);
String jsonResponse = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
System.out.println(jsonResponse);
} catch (UnsupportedEncodingException ex) {
System.out.println("API Unsupported instruction entry");
} catch (IOException ex) {
System.out.println("IO issue" + ex.getCause().getClass());
}
}
}
应该和上面有些类似。检查 RequestConfig 部分和异常处理部分。您可以在错误处理中添加一个标志并重试。我们在这里为 http 客户端设置超时值。您可以查看一些 java 文档以了解更多详细信息。
顺便说一下,您正在使用 POST 方法,根据定义它不是幂等的。因此,您在再次重新运行相同请求时需要小心。服务器可能已经在您超时后处理了。
我正在用 Java 调用 REST API,但响应有时会偶尔出现延迟。所以我想以某种方式检查响应是否花费了太长时间(例如 2 或 3 秒)和 restart/rerun 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";
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class Test {
public static void main(String[] args) {
int timeoutInMillis = 10;
RequestConfig config = RequestConfig.custom().
setConnectTimeout(timeoutInMillis).
setConnectionRequestTimeout(timeoutInMillis).
setSocketTimeout(timeoutInMillis).build();
final CloseableHttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(config).build();
HttpPost post = new HttpPost("https://reqres.in/api/api/users");
//this.post.setHeader("apikey", this.apikey);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new NameValuePair("name", "test1"));
parameters.add(new NameValuePair("job", "job1"));
try {
//post.setEntity(new UrlEncodedFormEntity(this.parameters));
HttpResponse response = client.execute(post);
String jsonResponse = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
System.out.println(jsonResponse);
} catch (UnsupportedEncodingException ex) {
System.out.println("API Unsupported instruction entry");
} catch (IOException ex) {
System.out.println("IO issue" + ex.getCause().getClass());
}
}
}
应该和上面有些类似。检查 RequestConfig 部分和异常处理部分。您可以在错误处理中添加一个标志并重试。我们在这里为 http 客户端设置超时值。您可以查看一些 java 文档以了解更多详细信息。
顺便说一下,您正在使用 POST 方法,根据定义它不是幂等的。因此,您在再次重新运行相同请求时需要小心。服务器可能已经在您超时后处理了。