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

时间:2018-04-04 05:18:07

标签: java automation web-api-testing

我刚刚执行了以下脚本:

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$2.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)

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

2 个答案:

答案 0 :(得分:2)

您的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问题。

答案 1 :(得分:1)

我能够通过你的主机获得主持人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,那么一切都还可以。您可以尝试我的代码并检查即将到来的响应。