neneItaly
neneItaly

Reputation: 273

groovy httpBuilder with proxy (and auth)

I'm trying to use httpBuilder to connect to a webservice using a proxy but am somehow unable to do so. I found a question which can potentially help to solve my problem (part of it at least) here: How to use HTTPBuilder behind a proxy with authentication But no valid answer was given.

What I basically do is: create a new httpBuilder passing my URL setting the proxy with the setProxy(host, port, 'http') method http.request(GET, XML){ parse XML and do stuff... }

Question 1: can I specify the host as hostname or only as IP address? I think so but wanted to check. Question 2: what if the proxy itself requires some auth?

I have it running ok on my dev machine which uses another network with no need of a proxy. When I deploy it to the pre-prod testing environment I get a "connection refused" exception.

Edit: I'm deploying the whole thing to a tomcat 6.39 server

Upvotes: 2

Views: 2297

Answers (1)

neneItaly
neneItaly

Reputation: 273

glad to say I found the issue and solved it on my own. Felt like it was a good idea to share it here as well.

The point was that, in a previous attempt to set the proxy, I edited the setenv.sh script for tomcat startup setting JAVA_OPTS to use the proxy (-Dhttp.proxyHost, proxyPort etc.). This did not help thus I moved to the approach of dynamically setting the proxy at runtime in the code (relying on an external properties file to store the config. Just to avoid hardcoding them in the source) with httpBuilder.

Apparently the two things together messed up the final proxy config! I removed the -Dhttp.proxyHost and other related JAVA_OPTS and it started working with httpBuilder out of the box.

Lessons learned (the hard way): do not expect a webapp running in tomcat to take the proxy settings from system or env variables. Better set it at runtime in the code (just for the call which needs the proxy) and externalise the proxy host and port to a regular properties file so that you don't have to recompile the sources everytime the proxy changes url or port

Upvotes: 1

Related Questions