AnsweredAssumed Answered

hcp-demo with http client connection

Question asked by Tej Kiran Sharma on Oct 3, 2016
Latest reply on Oct 13, 2016 by Henk Hindriks

Hi,

 

I am newbie for HCP. I am working on hcp-demo version. Where first of all I do not have enough information about the HCP.  Whatever I go through the documentation, I have a domain say hcp-demo.hcpdemo.com. Next when I create tenant then it gives me name https://tn2.hcp-demo.hcpdemo.com:8000 where tn2 is tenant name. Because I did not configure host name so, I am using IP in url. The following example is of http. I tried with https (SSL) also but same error.

 

Now when it am trying to connect this using my java client with the following code I am getting 403 error code.

String url = "https://192.168.1.128/rest/hello.txt";

String localFilePath = "C:\\Users\\Admin\\Desktop\\Document1.txt";

HttpClient client = HttpClientBuilder.create().build();
HttpPut request = new HttpPut(url);

request.addHeader("Authorization", "HCP dGVqa2lyYW4=:d41e98d1eafa6d6011d3a70f1a5b92f0");

request.addHeader("Host","tn2.hcp-demo.hcpdemo.com");

File input = new File(localFilePath);

byte[] fileAsByteArr = convertTobyteArr(input);
   ByteArrayEntity requestEntity = new ByteArrayEntity(fileAsByteArr);

   //set the request to use the byte array
   request.setEntity(requestEntity);

//execute PUT request
   HttpResponse response = null;

   try {

  response = client.execute(request);

  } catch (IOException e) {

  e.printStackTrace();

  }

   //print response status to console
   System.out.println("Response Code : "    + response.getStatusLine().getStatusCode() + " " +   response.getStatusLine().getReasonPhrase());

 

Using the above mentioned code, when I run I am getting following logs...

16:32:48.420 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default

16:32:48.449 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context

16:32:48.452 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://192.168.1.128:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]

16:32:48.499 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://192.168.1.128:80][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]

16:32:48.502 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {}->http://192.168.1.128:80

16:32:48.505 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to /192.168.1.128:80

16:32:48.508 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connection established 192.168.1.79:49278<->192.168.1.128:80

16:32:48.509 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Executing request PUT /rest/hello.txt HTTP/1.1

16:32:48.509 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED

16:32:48.514 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> PUT /rest/hello.txt HTTP/1.1

16:32:48.514 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Authorization: HCP dGVqa2lyYW4=:d41e98d1eafa6d6011d3a70f1a5b92f0

16:32:48.514 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: tn2.hcp-demo.hcpdemo.com

16:32:48.514 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 11

16:32:48.514 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive

16:32:48.514 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.1 (Java/1.8.0_20)

16:32:48.514 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate

16:32:48.514 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "PUT /rest/hello.txt HTTP/1.1[\r][\n]"

16:32:48.514 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Authorization: HCP dGVqa2lyYW4=:d41e98d1eafa6d6011d3a70f1a5b92f0[\r][\n]"

16:32:48.514 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: tn2.hcp-demo.hcpdemo.com[\r][\n]"

16:32:48.514 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 11[\r][\n]"

16:32:48.514 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"

16:32:48.515 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.1 (Java/1.8.0_20)[\r][\n]"

16:32:48.515 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"

16:32:48.515 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"

16:32:48.515 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Hello World"

16:32:48.517 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 403 Forbidden[\r][\n]"

16:32:48.518 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Mon, 03 Oct 2016 11:02:07 GMT[\r][\n]"

16:32:48.518 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: HCP V7.2.2.6[\r][\n]"

16:32:48.518 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Length: 0[\r][\n]"

16:32:48.518 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[\r][\n]"

16:32:48.524 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 403 Forbidden

16:32:48.524 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Mon, 03 Oct 2016 11:02:07 GMT

16:32:48.524 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: HCP V7.2.2.6

16:32:48.524 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 0

16:32:48.532 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection can be kept alive indefinitely

16:32:48.533 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection [id: 0][route: {}->http://192.168.1.128:80] can be kept alive indefinitely

16:32:48.533 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://192.168.1.128:80][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]

Response Code : 403 Forbidden

 

Please let us know, is there any header missing or else what I am missing..

Outcomes