对 HERE API 的地理编码请求随机失败

Geocoding requests to HERE API randomly fails

我正在尝试使用 HERE API 对地址进行地理编码。我不是免费计划。我尝试使用以下代码(Spring 在 Kotlin 中启动):

override fun geocode(address: Address): Coordinate? {
        val uriString = UriComponentsBuilder
            .fromHttpUrl(endpoint)
            .queryParam("app_id", appId)
            .queryParam("app_code", appCode)
            .queryParam("searchtext", addressToSearchText(address))
            .toUriString()
        logger.info("Geocode requested with url {}", uriString)
        val response = restTemplate.getForEntity(uriString, String::class.java)
        return response.body?.let {
            Klaxon().parse<GeocodeResponse>(it)
        }?.let {
            it.Response.View.firstOrNull()?.Result?.firstOrNull()
        }?.let {
            Coordinate(
                latitude = it.Location.DisplayPosition.Latitude,
                longitude = it.Location.DisplayPosition.Longitude
            )
        }.also {
            if (it == null) {
                logger.warn("Geocode failed: {}", response.body)
            }
        }
    }

原来,当我连续多次调用这个方法时,有些请求 returns 空响应,像这样:

{  
   "Response":{  
      "MetaInfo":{  
         "Timestamp":"2019-04-18T11:33:17.756+0000"
      },
      "View":[  

      ]
   }
}

我想不出为什么有些请求会失败的规则。好像是随机的。

但是,当我尝试在浏览器中使用 curl 调用相同的 URL 时,一切正常。

我想每秒请求数量有一些限制,但我在 HERE 文档中找不到任何内容。

有人知道这个限制吗?或者可能是其他原因?

实际上,我的代码有问题。对具有 "special" 符号(如 ü 和 ö)的地址的请求失败。问题出在构建请求 URL

val uriString = UriComponentsBuilder
            .fromHttpUrl(endpoint)
            .queryParam("app_id", appId)
            .queryParam("app_code", appCode)
            .queryParam("searchtext", addressQueryParam(address))
            .build(false)  // <= this was missed
            .toUriString()