4

I need to go through a proxy to get to my target server. It seems that I can do it with code like this:

def http = new HTTPBuilder( 'http://www.somesite.com')
http.setProxy('proxy.com', 8080, 'http')

but not like this:

System.setProperty("http.proxyHost", "proxy.com");
System.setProperty("http.proxyPort", "8080");
def http = new HTTPBuilder( 'http://www.somesite.com')

Shouldn't this work?


The real problem is that I'm using HTTPBuilder from within a Grails application and was expecting that starting Tomcat using -Dhttp.proxyHost and -Dhttp.proxyPort would let HTTPBuilder go through the proxy... but it is like HTTPBuilder is ignoring those JVM parameters.


It looks like the httpclient can be configured to use the JVM parameters like this: client.getHostConfiguration().setProxy(host, port) (from this stackoverflow question). Can this be done in HTTPBuilder (I don't know how to reference the underlaying httpclient)?

Community
  • 1
  • 1
zoran119
  • 10,657
  • 12
  • 46
  • 88

2 Answers2

0

You can also externalize the proxy params to Config.groovy, then set your proxy info via config.

Some examples of how to externalize configurations:

http://phatness.com/2010/03/how-to-externalize-your-grails-configuration/ http://www.comitservices.com/wp/?p=133

Then just grab your proxy info from config

def SomeService {
  def grailsApplication

  ..
    def http = new HTTPBuilder( 'http://www.somesite.com')
    http.setProxy(
      grailsApplication.config.proxyHost, 
      grailsApplication.config.proxyPort,
      grailsApplication.config.proxyScheme
    )
}
ikumen
  • 11,275
  • 4
  • 41
  • 41
-1

AFAIK, in context to Grails applications, we can provide the host and port for Tomcat itself but cannot define the behavior of how HTTP calls has to go through proxies during Server startup.

You would need add-proxy and set-proxy settings for your app to connect to somesite.com over the proxy, proxy.com. For example:

grails add-proxy client --host=proxy.com --port=8080 
       --username=guest --password=guest
grails set-proxy client

It is not tested by you can also follow this to see if you can use System.setProperty.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • That's fine for `run-app`, but I'm taking about a Grails app deployed to Tomcat. – zoran119 Jun 28 '13 at 01:38
  • Then I think Level 3 in the [link](http://grails.1312388.n4.nabble.com/Solution-to-Grails-Proxy-Issue-for-installing-plugins-on-windows-td2994191.html) provided in the answer is fit for your case. – dmahapatro Jun 28 '13 at 01:41