简体   繁体   English

Groovy HttpBuilder对NonProxyHosts的使用

[英]NonProxyHosts usage with Groovy HttpBuilder

If I create my httpBuilder as shown below (assume that a proxyUsername IS set, so setCredentials is called), then calls to httpAddress-es that are passed in properly are routed through the proxy. 如果我如下所示创建我的httpBuilder(假设已设置proxyUsername,则调用setCredentials),则正确传入的对httpAddress-es的调用将通过代理进行路由。 However, the Application has some http calls that are within the local network. 但是,该应用程序在本地网络中有一些http调用。 Can http.nonProxyHosts be used to work around this and bypass the Proxy? 可以使用http.nonProxyHosts解决此问题并绕过代理吗? If so, how? 如果是这样,怎么办? Use System.setProperty? 使用System.setProperty? Or something on HttpBuilder? 还是HttpBuilder上的东西?

    HTTPBuilder httpBuilder = new HTTPBuilder(httpAddress)
    httpBuilder.setProxy(webProxyHost, webProxyPort, webProxyProtocol)
    if (proxyUsername) {
        httpBuilder.client.getCredentialsProvider().setCredentials(
                    new AuthScope(webProxyHost, webProxyPort),
                    new UsernamePasswordCredentials(proxyUsername, proxyPassword))
        }
    }

In the code above, all of the various named elements (webProxyHost, etc) are declared as String and set accordingly. 在上面的代码中,所有各种命名元素(webProxyHost等)都声明为String并进行相应设置。

In answer to the question in the above comment, our primary 'nonProxyHost' need was for 'localhost' which is there by default. 为了回答以上评论中的问题,我们的主要“ nonProxyHost”需求是默认情况下的“ localhost”。 Thus this ceased to be an issue. 因此,这不再是一个问题。 Did not ever really find out how to accomplish this as it is somewhat version-specific on HttpClient. 从未真正发现如何完成此操作,因为它在HttpClient上是特定于版本的。

You can set the System property: 您可以设置系统属性:

System.setProperty('http.nonProxyHosts', myNonProxyHosts)

However, if you call 'setProxy' on HttpBuilder, even if you call 'useSystemProperties' it will not. 但是,如果您在HttpBuilder上调用“ setProxy”,即使您调用“ useSystemProperties”也不会。 This is in their documentation, just not obvious! 这在他们的文档中,只是不明显!

Finally, you might be able to call: 最后,您可以致电:

httpBuilder.client.params.setParameter('http.nonProxyHosts', myNonProxyHosts)

But I do not know for sure if that is the property name and documentation of those properties is hard to find. 但是我不确定这是否是属性名称,并且很难找到这些属性的文档。 Worse - those 'params' are deprecated - you are supposed to use the better 'config' classes, though once again finding comprehensive documentation on all the parameters for that is not the easiest! 更糟糕的是-不推荐使用这些“参数”,您应该使用更好的“配置”类,尽管要再次找到有关所有参数的全面文档并不是最简单的! Wish I could have been of more help! 希望我能提供更多帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM