【问题标题】:Using Jodd HTTP library behind proxy在代理后面使用 Jodd HTTP 库
【发布时间】:2017-11-21 11:35:31
【问题描述】:

我刚刚接到了一个使用 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()
}

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

【问题讨论】:

  • 我的回答对你有用吗?只是想知道您是否需要更多帮助...

标签: java groovy proxy jodd


【解决方案1】:

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 和 SOCKE5 代理类型。

请参阅documentation

【讨论】:

  • 谢谢你的回答,我要到星期一才有机会尝试这个,但我会尽快通知你,再次感谢?
  • @Richlewis,你是怎么做到的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
  • 2017-04-23
  • 2016-12-08
  • 1970-01-01
  • 2016-08-06
相关资源
最近更新 更多