简体   繁体   中英

How to fix HTTP 504 Gateway timeout exception

I am attempting a GET request to another API to get a json response.

When I make the request with the code below, I am getting HTTP 504 Error (Gateway timeout error).

However, when I tried it through rest client tool, the request does not throw any error.

How do I increase the time gap in my code to avoid the timeout error?

This is how the call looks like:

HttpClient httpClient = getBasicAuthDefaultHttpClient();
String url= "http://XXXXX";
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("id", id);
httpGet.addHeader("secret", secret);
httpGet.addHeader("network_val", networkval);

HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();

if (entity != null && response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
    ObjectMapper objectMapper = new ObjectMapper();
    restInfo = objectMapper.readValue(entity.getContent(), MyClass.class);
} else {
    logger.error("Call to API failed: response code = {}", response.getStatusLine().getStatusCode());
}

Please note:

Could this be something to do with 'https'?

When I try with 'http' through my Insomnia REST Client I get the ERROR: error: Failure when receiving data from the peer. https works fine without any error ( https://XXXXX )

This is what I tried.

public HttpClient getBasicAuthDefaultHttpClient() {
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, 
    password);
    provider.setCredentials(AuthScope.ANY, creds);

    //Fix to avoid HTTP 504 ERROR (GATEWAY TIME OUT ERROR)
    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder.setConnectTimeout(30 * 1000);
    requestBuilder.setConnectionRequestTimeout(30 * 1000);

    HttpClientBuilder builder = HttpClientBuilder.create();
    builder.setDefaultRequestConfig(requestBuilder.build());
    builder.setDefaultCredentialsProvider(provider).build();

    return builder.build();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM