2

I got a problem with a httpPost from a Java-Application which is running on HCP-Trial-Account in a Tomcat7-Container. I use HttpClient 4.5.3.

The code runs on my local Tomcat7-Server fine and works. However, if deploy it to the HCP there occurs a problem.

public static Notebook getAllNotebooks(String code, String redirectUri) throws IOException, URISyntaxException{ ClassLoader classLoader = Connection.class.getClassLoader(); URL resource = classLoader.getResource("org/apache/http/impl/client/HttpClientBuilder.class"); String returnUri = "https://login.live.com/oauth20_token.srf"; HttpPost tokenRequest = new HttpPost(returnUri); HttpClient client = new DefaultHttpClient();//HttpClientBuilder.create().build(); //Exception: http://stackoverflow.com/questions/22330848/httpclient-example-exception-in-thread-main-java-lang-nosuchfielderror-inst tokenRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded"); tokenRequest.setEntity(new UrlEncodedFormEntity(Connection.getParametersForURLBody(code, redirectUri), Consts.UTF_8)); tokenRequest.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0"); HttpResponse tokenResponse = client.execute(tokenRequest); //Here it gets stuck 

The problem occurs when the tokenRequest is executed. The application gets stuck and the tokenRequest runs forever.

Here are the threads that are running forever which I can see in debug mode:

SAP JVM Debug Target Daemon Thread [NioBlockingSelector.BlockPoller-1] (Running) Daemon Thread [NioBlockingSelector.BlockPoller-2] (Running) Daemon Thread [RMI TCP Connection(1)-127.0.0.1] (Running) Thread [Timer-0] (Running) Daemon Thread [RMI TCP Connection(2)-10.117.35.76] (Running) Thread [pool-1-thread-1] (Running) Thread [Timer-1] (Running) Daemon Thread [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (Running) Daemon Thread [JCoTimeoutChecker] (Running) Daemon Thread [http-bio-8001-Acceptor-0] (Running) Daemon Thread [http-bio-8001-AsyncTimeout] (Running) Daemon Thread [http-nio-127.0.0.1-9001-ClientPoller-0] (Running) Daemon Thread [http-nio-127.0.0.1-9001-Acceptor-0] (Running) Daemon Thread [http-nio-8041-ClientPoller-0] (Running) Daemon Thread [http-nio-8041-Acceptor-0] (Running) Daemon Thread [ajp-bio-8009-Acceptor-0] (Running) Daemon Thread [ajp-bio-8009-AsyncTimeout] (Running) Daemon Thread [RMI TCP Connection(10)-127.0.0.1] (Running) Daemon Thread [RMI TCP Connection(11)-10.117.10.34] (Running) Thread [main] (Running) Daemon Thread [http-nio-8041-exec-1] (Running) Daemon Thread [http-nio-8041-exec-2] (Running) Daemon Thread [http-nio-8041-exec-3] (Running) Daemon Thread [http-nio-8041-exec-4] (Running) Daemon Thread [http-nio-8041-exec-5] (Running) Thread [pool-2-thread-1] (Running) Daemon Thread [http-nio-8041-exec-6] (Stepping) Daemon Thread [http-nio-8041-exec-7] (Running) Daemon Thread [http-nio-8041-exec-8] (Running) Daemon Thread [http-nio-8041-exec-9] (Running) Daemon Thread [http-nio-8041-exec-10] (Running) Daemon Thread [RMI TCP Connection(idle)] (Running) Daemon Thread [RMI TCP Connection(idle)] (Running) 

The program gets stucked at this point.

I have no clue what to do and would really appreciate some hints and help :).

Greetings Maverin

3
  • Most probably you need to configure a proxy. See the documentation referenced in the given answer. Commented Feb 27, 2017 at 19:46
  • Thats all fine, but the examples are all with UrlConnection, I don´t know how to execute it with httpclient. Would be really thankfull for some more help :). Commented Feb 28, 2017 at 12:50
  • You can check hc.apache.org/httpcomponents-client-ga/examples.html for an example with apache http client and proxy Commented Feb 28, 2017 at 14:15

2 Answers 2

0

Normally, you should use the Connectivity service in order to access Internet resources from within HCP-deployed applications. I guess that trying to do it directly causes you app to freeze / crash. Might also be something else that I am missing.

You can find info about doing this in the official documentation: help.sap.com.

In a nutshell, you will have to:

  • Create a destination (either in your HCP account or directly in the Java app) towards the given site.
  • Because you use HTTPS, you will most likely have to import the certificate of the remote host into the trust store (docu link).
  • Declare the connectivity configuration in the web.xml.
  • Use the JNDI (i.e. InitialContext) to obtain the connectivity configuration in your java code. This configuration can be then used to obtain an URL towards the remote resource. You can use this URL to either open a direct connection or to pass it to the HttpClient (the URL class has a toURI method).
Sign up to request clarification or add additional context in comments.

3 Comments

In the HCP Tomcat7 runtime the connectivity service is mainly a secure storage for configurations (i.e. URL, user/password etc). All the stuff like http client generation, trust store configuration etc is done by the application.
I followed this example: help.hana.ondemand.com/help/… But I don´t know how to do it with httpclient instead of ur-connection?
The problem is, I tried all the examples for tomcat 7, but I really don´t know how to do it with httpclient instead of url-connection.
0

I've had this problem too, and in the end, I found a solution that works with Tomcat8 aswell. The key is to make the connection with a builder and then use useSystemProperties(). This way you get the proxy properties thats needed for the HCP. The extra stuff with the connectionmanager isn't strictly needed, but it helps with the performance. The param-syncro is legacy stuff.

org.apache.http.impl.conn.PoolingHttpClientConnectionManager cm public static HttpClient getHttpClient(){ if(cm == null){ synchronized(params){ cm = new PoolingHttpClientConnectionManager(); // Increase max total connection to 200 cm.setMaxTotal(200); // Increase default max connection per route to 20 cm.setDefaultMaxPerRoute(200); } } org.apache.http.impl.client.CloseableHttpClient httpClient = HttpClients.custom() .setConnectionManager(cm) .useSystemProperties() .build(); return httpClient; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.