Using Jodd HTTP library behind proxy

408 Views Asked by At

I have just been handed a groovy project that is using the Jodd library (I have little experience with this). I am looking to find out how you setup configuration so that http and https calls can be made behind a company proxy.

At the moment a helper class has been setup

#! /usr/bin/groovy
package org.myOrg

import groovy.json.JsonBuilder
@Grab("org.jodd:jodd-http:3.8.5")
import jodd.http.HttpRequest

/**
 * Helper class for making REST calls from a Jenkins Pipeline job.
*/
class JenkinsHttpClient {
// Constants
private static final String USER_AGENT = "User-Agent";
private final HttpRequest httpRequest
private final String userAgent = 'Jenkins'

JenkinsHttpClient() {
    httpRequest = new HttpRequest()
}

/**
 * GET method
 * @param url - This is the endpoint
 * @return response body as String
 */
private def get(String url) {
    def resp = httpRequest.get(url)
            .header(USER_AGENT, userAgent)
            .send()
    return resp.bodyText()
}

How or where do I add config so that this will work behind a proxy?

1

There are 1 best solutions below

2
igr On

HttpConnectionProvider also allows you to specify the proxy. Just provide the ProxyInfo instance with the information about the used proxy (type, address, port, username, password):

SocketHttpConnectionProvider scp = new SocketHttpConnectionProvider();
scp.useProxy(ProxyInfo.httpProxy("proxy_url", 1090, null, null));

HttpResponse response = HttpRequest
    .get("http://jodd.org/")
    .withConnectionProvider(scp)
    .send();

Jodd supports HTTP, SOCKS4 and SOCKE5 proxy types.

See the documentation.