执行Rest API基础自动化脚本时遇java.net.UnknownHostException求助
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 runping 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's8.8.8.8or Cloudflare's1.1.1.1and 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 yourRestAssured.baseURIis exactlyhttps://maps.googleapis.com— no typos like missing thesinhttps, or misspellinggoogle. Even a tiny typo will cause the hostname to be unresolvable.
内容的提问来源于stack exchange,提问作者Baburaj V

