如何修复HTTP 504网关超时异常

时间:2019-10-14 18:24:35

标签: java http http-headers http-status-code-504

我正在尝试向另一个API发出GET请求以获取json响应。

当我使用下面的代码发出请求时,我收到HTTP 504错误(网关超时错误)。

但是,当我通过rest client工具尝试该请求时,该请求不会引发任何错误。

如何增加代码中的时间间隔以避免超时错误?

通话内容如下:

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());
}

请注意:

这与“ https”有关吗?

当我通过Insomnia REST Client通过'http'尝试时,出现错误:错误:从对等方接收数据时失败。 https正常运行,没有任何错误(https://XXXXX

1 个答案:

答案 0 :(得分:0)

这就是我尝试过的。

import app from 'firebase/app'
import 'firebase/auth'
import 'firebase/database'
import 'firebase/firestore'

const config = {
  apiKey: process.env.REACT_APP_API_KEY,
  authDomain: process.env.REACT_APP_AUTH_DOMAIN,
  databaseURL: process.env.REACT_APP_DATABASE_URL,
  projectId: process.env.REACT_APP_PROJECT_ID,
  storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID 
}

class Firebase {
  constructor (props) {
    app.initializeApp(config)

    this.auth = app.auth()
    this.db = app.database()
    this.firestore = app.firestore()
  }

  signUpWithEmailAndPassword = (email, pw) => {
    this.auth.createUserWithEmailAndPassword(email, password)
    .then(registeredUser => {
      this.firestore.collection("usersCollection")
      .add({
        uid: registeredUser.user.uid,
        field: 'Info you want to get here',
        anotherField: 'Another Info...',
        ....
      })
    }
  }
}