在代理后面使用JODD HTTP库



我刚刚交出了一个正在使用jodd库(我对此的经验很少)的Groovy项目。我希望找出您如何设置配置,以便可以在公司代理后面进行HTTP和HTTPS调用。

目前已经设置了一个助手类

#! /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()
}

我如何或在哪里添加配置,以便在代理后面工作?

HttpConnectionProvider还允许您指定代理。只需向ProxyInfo实例提供有关使用的代理的信息(类型,地址,端口,用户名,密码):

SocketHttpConnectionProvider scp = new SocketHttpConnectionProvider();
scp.useProxy(ProxyInfo.httpProxy("proxy_url", 1090, null, null));
HttpResponse response = HttpRequest
    .get("http://jodd.org/")
    .withConnectionProvider(scp)
    .send();

JODD支持HTTP,Socks4和MECKE5代理类型。

请参阅文档。

最新更新