在执行 Rest Api 的基本自动化脚本时获取 java.net.UnknownHostException

Getting java.net.UnknownHostException while executing the basic automation script for Rest 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);  
 }
}

但是我在 运行 脚本时遇到异常。

Exception in thread "main" java.net.UnknownHostException: maps.googleapis.com
 at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
 at java.net.InetAddress.lookupAllHostAddr(Unknown Source)
 at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
 at java.net.InetAddress.getAllByName0(Unknown Source)
 at java.net.InetAddress.getAllByName(Unknown Source)
 at java.net.InetAddress.getAllByName(Unknown Source)
 at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
 at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:263)
 at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
 at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)
 at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)

有人可以帮我解决这个问题吗?

我能够通过您的主机https://maps.googleapis.com得到回复 代码。

您可以启动 UNIX 终端并使用 nslookup 命令(以及其他命令)查看您的 DNS 服务器是否可以成功将主机名解析为 IP 地址。

尝试$ nslookup https://maps.googleapis.com

RestAssured.baseURI = "https://maps.googleapis.com";
RequestSpecification httpRequest = given();

Response response = httpRequest.
        param("location", "-33.8670522,151.1957362").
        param("radius", "500").
        param("types", "food").
        param("key", "AIzaSyDXR2YGnRu3-112ttezUsQ-T2zOkb6aafE").
        when().
        get("/maps/api/place/nearbysearch/json"); 

System.out.println("Response Body is =>  " + response.asString());

更新

$ nslookup https://maps.googleapis.com

Server: questgedctvm.com
Address: 10.197.72.16

Non-authoritative answer:
Name:    googleapis.l.google.com
Addresses: <IP_Addresses>

Aliases:  https://maps.googleapis.com

检查 IP_Address,如果您能够获得主机 https://maps.googleapis.com 的 IP_Address,则一切正常。您可以尝试我的代码并检查即将到来的响应。

您的 DNS 可能有问题。如果你这样做 nslookup maps.googleapis.com,你应该或多或少看到以下内容:

Server:  questgedctvm.com
Address:  10.197.72.16 

Non-authoritative answer:
Name:    googleapis.l.google.com
Addresses:  2a00:1450:4002:805::200a
          216.58.205.74
          216.58.205.106
          216.58.205.138
          216.58.205.170
          216.58.205.202
          216.58.198.10
          216.58.198.42
          172.217.23.106
          216.58.205.42
Aliases:  maps.googleapis.com

如果这不是您所看到的(实际 IP 地址可能不同,但您应该得到一堆),那么您需要解决 DNS 问题。这不是 Java 问题。