You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Rest API基础自动化脚本时遇java.net.UnknownHostException求助

java.net.UnknownHostException when running RestAssured script for Google Places API

Problem Description

I executed the following RestAssured script to call the Google Places Nearby Search API:

import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;
public class Basics {
    public static void main(String[] args) {
        RestAssured.baseURI="https://maps.googleapis.com";
        given().
            param("location","-33.8670522,151.1957362").
            param("radius","500").
            param("types","food").
            param("key","AIzaSyDXR2YGnRu3-112ttezUsQ-T2zOkb6aafE").
        when().
            get("/maps/api/place/nearbysearch/json").
        then().assertThat().statusCode(200);
    }
}

But during execution, I got a java.net.UnknownHostException error. I need help resolving this issue.

Solutions

Let's troubleshoot this step by step — this exception usually means your Java program can't resolve the hostname maps.googleapis.com or can't reach it over the network:

  • Verify basic network connectivity
    First, open the full API URL directly in your browser (using the exact parameters from your script):
    https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&key=AIzaSyDXR2YGnRu3-112ttezUsQ-T2zOkb6aafE
    If the browser can't load the response, your network might be blocking access to Google's APIs (e.g., regional restrictions, corporate firewall). You may need to check network policies or adjust your connection settings.

  • Check DNS resolution
    Open your command line and run ping maps.googleapis.com. If you get a "could not find host" error, your DNS server isn't resolving the hostname correctly. Try switching to a public DNS like Google's 8.8.8.8 or Cloudflare's 1.1.1.1 and retry the ping test.

  • Configure proxy settings (if behind a corporate proxy)
    If your network uses a proxy server, you need to tell RestAssured to use it. Add this line before your request setup:

    RestAssured.proxy("your-proxy-host", your-proxy-port);
    

    If the proxy requires authentication, use this modified request structure:

    given()
        .proxy("your-proxy-host", your-proxy-port)
        .proxyCredentials("proxy-username", "proxy-password")
        .param("location","-33.8670522,151.1957362")
        // rest of your parameters...
    
  • Double-check the baseURI spelling
    Make sure your RestAssured.baseURI is exactly https://maps.googleapis.com — no typos like missing the s in https, or misspelling google. Even a tiny typo will cause the hostname to be unresolvable.

内容的提问来源于stack exchange,提问作者Baburaj V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:34:02