有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中,如果响应时间超过n秒,api是否重新运行http post请求?

我正在用Java调用RESTAPI,但有时响应中会偶尔出现延迟。因此,我想以某种方式检查响应是否花费了太长的时间(例如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";
}

共 (1) 个答案

  1. # 1 楼答案

    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方法,根据定义,它不是幂等的。因此,在再次运行相同的请求时,需要小心。服务器可能已经在您获得超时后处理了