在Neo4j远程服务器上使用Apache HttpClient的UnknownHostException

时间:2015-10-30 00:59:49

标签: rest neo4j apache-httpclient-4.x

我正在尝试向GrapheneDB上的Neo4j远程实例发送REST调用。它以前工作过,现在,在用HttpClient进行一些重构后,它不再起作用了。

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(
                    "graph",
                    "titi"));

    try (CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(credsProvider)
            .build()) {


        // target url
        HttpHost targetHost = new HttpHost("http://graph.sb01.stations.graphenedb.com:24444");
        HttpPost postRequest = new HttpPost("/db/data/ext/SpatialPlugin/graphdb/addEditableLayer");

        // header
        postRequest.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8).toString());
        postRequest.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());

        // content
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("layer", "geom");
        jsonObj.put("format", "WKT");
        jsonObj.put("nodePropertyName", "position");

        StringEntity stringEntity = new StringEntity(jsonObj.toString(), StandardCharsets.UTF_8);
        stringEntity.setContentType(ContentType.APPLICATION_JSON.toString());
        postRequest.setEntity(stringEntity);

        HttpResponse httpResponse = httpClient.execute(targetHost, postRequest);
        HttpEntity response = httpResponse.getEntity();

    } catch (Exception e) {
        logger.error("unable to add WKT layer for 'position'", e);
    }

这里发生了什么:

java.net.UnknownHostException: http://graph:toto@graph.sb01.stations.graphenedb.com:24444
at java.net.InetAddress.getAllByName0(InetAddress.java:1280) ~[na:1.8.0_60]
at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_60]
at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_60]
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:111) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:117) ~[httpclient-4.5.1.jar:4.5.1]
at controllers.AdminController.createGraphMetaData(AdminController.java:140) [classes/:na]
at controllers.AdminController.reset(AdminController.java:56) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
at ninja.params.ControllerMethodInvoker.invoke(ControllerMethodInvoker.java:57) [ninja-core-5.2.1.jar:na]
at ninja.FilterChainEnd.next(FilterChainEnd.java:49) [ninja-core-5.2.1.jar:na]
at filters.LogFilter.filter(LogFilter.java:40) [classes/:na]
at ninja.FilterChainImpl.next(FilterChainImpl.java:35) [ninja-core-5.2.1.jar:na]
at ninja.NinjaDefault.onRouteRequest(NinjaDefault.java:102) [ninja-core-5.2.1.jar:na]
at ninja.servlet.NinjaServletDispatcher.service(NinjaServletDispatcher.java:86) [ninja-servlet-5.2.1.jar:na]
at com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:287) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:277) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:182) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:85) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:119) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:133) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:130) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.GuiceFilter$Context.call(GuiceFilter.java:203) [guice-servlet-4.0.jar:na]
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:130) [guice-servlet-4.0.jar:na]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) [jetty-servlet-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) [jetty-servlet-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) [jetty-server-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [jetty-server-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.server.Server.handle(Server.java:497) [jetty-server-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) [jetty-server-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [jetty-io-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.10.v20150310.jar:9.2.10.v20150310]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) [jetty-util-9.2.10.v20150310.jar:9.2.10.v20150310]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_60]

我可以使用相同的网址访问Neo4j管理员网页:http://graph:toto@graph.sb01.stations.graphenedb.com:24444 =>行

我可以在此网址上执行nslookup =>行

我可以使用Chrome中的Advanced Rest Client调用此REST服务=>行

HttpClient有什么问题?

1 个答案:

答案 0 :(得分:1)

好的抱歉,我找到了。我确信我已经尝试了这个但是......

HttpHost targetHost = new HttpHost("graph.sb01.stations.graphenedb.com", 24444);

HttpHost targetHost = HttpHosts.create("http://graph.sb01.stations.graphenedb.com:24444");