# java.net.UnknownHostException using Java Rest client 5.6.3

**URL:** <https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329>\
**Category:** Elasticsearch\
**Created:** [November 3, 2017, 3:05pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329 "2017-11-03T15:05:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 3, 2017, 3:05pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/1 "2017-11-03T15:05:13Z")

</div>

I keep getting java.net.UnknownHostException for connecting to my host elasticsearch.  
I have not completed the actual search body yet, because I am getting unknown host error.

I figure I can solve that problem later.

Here is the code, any reason why I cannot reach my cluster?

```
Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
                    new BasicHeader("Role", "Read") };
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "password"));

RestClient lowLevelRestClient = RestClient.builder(
            new HttpHost("https://xxxxxxxxx:9243")).setDefaultHeaders(headers).build();

RestHighLevelClient client =
    	    new RestHighLevelClient(lowLevelRestClient);

String thePath = "index_name";
    try {
    	thePath = URLEncoder.encode(thePath, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    } 

SearchRequest searchRequest = new SearchRequest(thePath);
SearchResponse searchResponse = null;
    try {
    	searchResponse = client.search(searchRequest);
    } catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }

    		
    	}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 3, 2017, 3:45pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/2 "2017-11-03T15:45:05Z")

</div>

I believe you should create an http host like this instead:

```
new HttpHost("xxxxxxxxx", 9243, "https")

```

See [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/\_encrypted\_communication.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_encrypted_communication.html)

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 3, 2017, 5:06pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/3 "2017-11-03T17:06:13Z")

</div>

same error.  
Any ideas?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 3, 2017, 5:21pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/4 "2017-11-03T17:21:22Z")

</div>

Did you apply everything which is in [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/\_encrypted\_communication.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_encrypted_communication.html) ?

What are the plugins you are using? Can you share elasticsearch logs?

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 3, 2017, 5:53pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/5 "2017-11-03T17:53:13Z")

</div>

No, But I am a bit confused.

The docs say to instantiate a high level rest object with a low level rest object.  
Problem is the documentation from your link doesn't use the low level rest client at all.

Should I just use what's in your link?

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 3, 2017, 6:13pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/6 "2017-11-03T18:13:33Z")

</div>

How can I share the logs with you?  
Is there an export?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 6, 2017, 6:52am UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/7 "2017-11-06T06:52:38Z")

</div>

When you start elasticsearch, it produces logs. Just share them here. Don't forget to format them.

> Problem is the documentation from your link doesn't use the low level rest client at all.

Isn't that `RestClientBuilder builder = RestClient.builder(...)` a Low Level client?

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 7, 2017, 2:35pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/8 "2017-11-07T14:35:27Z")

</div>

Here is the doc:

> **[Initialization | Java REST Client \[7.17\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-initialization.html)**

Also, I am using elastic cloud.  
My logs appear like this:

 ![es_cloud_logs](https://us1.discourse-cdn.com/elastic/original/3X/6/3/6356356e960459129b3c0080f823d14d521f3117.jpg)

When I click into the logs, there's nothing useful.

> "No results found in the last week's logs."

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 9, 2017, 12:09am UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/9 "2017-11-09T00:09:28Z")

</div>

Last time I built such a client for elastic cloud service, I wrote:

> <https://github.com/dadoonet/fscrawler/blob/master/src/main/java/fr/pilato/elasticsearch/crawler/fs/client/ElasticsearchClient.java#L271-L288>

I did not test it it recently though.

Quick question: are you able to do it from the command line using curl for example?

I mean are you sure you have the rights to connect to the cluster?

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 9, 2017, 4:41pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/10 "2017-11-09T16:41:20Z")

</div>

I have not tried CURL.

This is what I have now:

```
Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
                new BasicHeader("Role", "Read") };
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
		

		RestClient r = RestClient.builder(new HttpHost("xxxxxxxxxxxxxx.us-east-1.aws.found.io",9243,"https")).setDefaultHeaders(headers).build();
		
		
		Map<String, String> params = Collections.singletonMap("pretty", "true");
		Response response = r.performRequest("GET", "/", params);
		
		System.out.println(response.toString());
		
		try {
			r.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

```

I get the following error:

> Exception in thread "main" org.elasticsearch.client.ResponseException: GET [https://xxxxxxxx.us-east-1.aws.found.io:9243/?pretty=true:](https://xxxxxxxx.us-east-1.aws.found.io:9243/?pretty=true:) HTTP/1.1 401 Unauthorized  
> {  
> "error" : {  
> "root\_cause" : [  
> {  
> "type" : "security\_exception",  
> "reason" : "action [cluster:monitor/main] requires authentication",  
> "header" : {  
> "WWW-Authenticate" : "Basic realm="security" charset="UTF-8""  
> }  
> }  
> ],  
> "type" : "security\_exception",  
> "reason" : "action [cluster:monitor/main] requires authentication",  
> "header" : {  
> "WWW-Authenticate" : "Basic realm="security" charset="UTF-8""  
> }  
> },  
> "status" : 401  
> }
> 
> at org.elasticsearch.client.RestClient$1.completed(RestClient.java:354)  
> at org.elasticsearch.client.RestClient$1.completed(RestClient.java:343)  
> at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119)  
> at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177)  
> at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436)  
> at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326)  
> at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)  
> at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)  
> at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)  
> at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:121)  
> at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)  
> at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)  
> at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)  
> at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)  
> at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)  
> at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)  
> at java.lang.Thread.run(Unknown Source)

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 9, 2017, 5:32pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/11 "2017-11-09T17:32:47Z")

</div>

Can you try with curl?

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 9, 2017, 5:39pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/12 "2017-11-09T17:39:10Z")

</div>

Curls works:

`curl -u username:password 'https://xxxxxxxxxxxxxxxxx.us-east-1.aws.found.io:9243/index_name/_search?scroll=1m&size=50&pretty' -d '{"query" : {"match_all" : {}}}'`

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 9, 2017, 6:06pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/13 "2017-11-09T18:06:09Z")

</div>

Can you also curl [https://xxxxxxxx.us-east-1.aws.found.io:9243/?pretty=true](https://xxxxxxxx.us-east-1.aws.found.io:9243/?pretty=true)

Same user as the ones you put in the rest Client

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 9, 2017, 6:08pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/14 "2017-11-09T18:08:53Z")

</div>

Yes, works fine:

{  
"name" : "instance-0000000000",  
"cluster\_name" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",  
"cluster\_uuid" : "xxxxxxxxxxxxxxxxxxxxxxxxx",  
"version" : {  
"number" : "5.6.3",  
"build\_hash" : "xxxxxx",  
"build\_date" : "2017-10-06T20:33:39.012Z",  
"build\_snapshot" : false,  
"lucene\_version" : "6.6.1"  
},  
"tagline" : "You Know, for Search"  
}

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 9, 2017, 7:24pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/15 "2017-11-09T19:24:45Z")

</div>

FYI:

My Java is run from my desktop.

The curl is run from an EC2 instance, ubuntu.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 9, 2017, 7:36pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/16 "2017-11-09T19:36:43Z")

</div>

Can you try from the same platform please?

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 9, 2017, 7:48pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/17 "2017-11-09T19:48:59Z")

</div>

Sure, this is what I get:

```
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.elasticsearch.client.ResponseException: GET https://xxxxxxxxxxxx:9243/?pretty=true: HTTP/1.1 401 Unauthorized
{
  "error" : {
    "root_cause" : [
      {
        "type" : "security_exception",
        "reason" : "action [cluster:monitor/main] requires authentication",
        "header" : {
          "WWW-Authenticate" : "Basic realm=\"security\" charset=\"UTF-8\""
        }
      }
    ],
    "type" : "security_exception",
    "reason" : "action [cluster:monitor/main] requires authentication",
    "header" : {
      "WWW-Authenticate" : "Basic realm=\"security\" charset=\"UTF-8\""
    }
  },
  "status" : 401
}

        at org.elasticsearch.client.RestClient$1.completed(RestClient.java:354)
        at org.elasticsearch.client.RestClient$1.completed(RestClient.java:343)
        at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:119)
        at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:177)
        at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436)
        at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326)
        at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
        at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
        at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
        at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:121)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
        at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
        at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
        at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
        at java.lang.Thread.run(Thread.java:748)
```

---

<div class="post-metadata">

**Author:** ![mc1392](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@mc1392](https://discuss.elastic.co/u/mc1392)\
**Post date:** [November 10, 2017, 11:54am UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/18 "2017-11-10T11:54:47Z")

</div>

Problem solved!  
@dadoonet thanks for the help, again!!!

Final issue to solve was my Authorization header.

Here is the working code:

```
String encodedBytes = Base64.getEncoder().encodeToString("username:password".getBytes());
		Header[] headers = { new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
				new BasicHeader("Authorization","Basic "+encodedBytes) };
		
		RestClient r = RestClient
				.builder(new HttpHost("xxxxxxxxxxx", 9243, "https"))
				.setDefaultHeaders(headers).build();

		Map<String, String> params = Collections.singletonMap("pretty", "true");
		Response response = r.performRequest("GET", "/", params);

		System.out.println(response.toString());

		try {
			r.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 10, 2017, 12:35pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/19 "2017-11-10T12:35:11Z")

</div>

Glad you solved it. I wanted to test it but traveling ATM so hard to do it. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 8, 2017, 12:35pm UTC](https://discuss.elastic.co/t/java-net-unknownhostexception-using-java-rest-client-5-6-3/106329/20 "2017-12-08T12:35:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
