让 JRuby 继承 Java 代理设置

发布于 2024-10-15 03:56:32 字数 123 浏览 6 评论 0原文

我想从 JRuby 之上运行的 Rails 代码发出 HTTP 请求。

如何让它重用给运行它的 JVM 的 http.proxyHost、http.proxyPort 和 http.nonProxyHosts 设置?

I would like to make HTTP requests from Rails code running on top of JRuby.

How can I make it to re-use http.proxyHost, http.proxyPort and http.nonProxyHosts settings, given to JVM running it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2024-10-22 03:56:32

要通过 JRuby 传递 JVM 标志,请使用 -J...。在这种情况下:

jruby -J-Dhttp.proxyHost=foo -J-Dhttp.proxyPort=1234 -J-Dhttp.nonProxyHosts="*.bar.com" ...

JRuby 的帮助文本对此进行了解释。

-J[java option] pass an option on to the JVM (e.g. -J-Xmx512m)
                use --properties to list JRuby properties
                run 'java -help' for a list of other Java options

To pass JVM flags through JRuby, use -J.... In this case:

jruby -J-Dhttp.proxyHost=foo -J-Dhttp.proxyPort=1234 -J-Dhttp.nonProxyHosts="*.bar.com" ...

This is explained in JRuby's help text.

-J[java option] pass an option on to the JVM (e.g. -J-Xmx512m)
                use --properties to list JRuby properties
                run 'java -help' for a list of other Java options
相对绾红妆 2024-10-22 03:56:32

我也有同样的问题。我发现 java 或 net::http 不遵守 nonProxyHosts 选项。解决这个问题的最佳方法是修改 ENV_JAVA 设置来解决这个问题。

我为确保使用 nonProxyHosts 所采取的步骤如下:

1) JAVA_OPTS="-Dhttp.proxyHost=192*** -Dhttp.proxyPort=1234 -Dhttp.nonProxyHosts=local|127.0.0.1"
OR
1) JRUBY_OPTS="-J-Dhttp.proxyHost=192*** -J-Dhttp.proxyPort=1234 -J-Dhttp.nonProxyHosts=local|127.0.0.1"

请记住,至少对于 java1.7,nonProxyHosts 不应包含引号,请参阅 此处

现在我发现 net::http 或 java 本身实际上并不支持 nonProxyHosts 选项。

不过,您可以通过在 JRuby 中执行以下操作来解决此问题,

a = URI("http://someurl")
Net::HTTP.new(a).proxy?.should == true
regex = /$#{ENV_JAVA["http.nonProxyHosts"]}/ #dollar needed to behave as expected
if a.hostname.match(regex)
   ENV_JAVA["http.proxyHost"]=nil
end
Net::HTTP.new(a).proxy?.should == false

希望有所帮助。

I have had the same issue. I found that java or net::http doesn't obey the nonProxyHosts option. The best way to get around this is to modify the ENV_JAVA settings to account for this.

The steps I took to ensure nonProxyHosts was used were the following:

1) JAVA_OPTS="-Dhttp.proxyHost=192*** -Dhttp.proxyPort=1234 -Dhttp.nonProxyHosts=local|127.0.0.1"
OR
1) JRUBY_OPTS="-J-Dhttp.proxyHost=192*** -J-Dhttp.proxyPort=1234 -J-Dhttp.nonProxyHosts=local|127.0.0.1"

Keep in mind that at least for java1.7 the nonProxyHosts should not have quotations see here.

Now I find that either net::http or java itself doesn't actually honour the nonProxyHosts option.

However you can get around this by doing the following in JRuby

a = URI("http://someurl")
Net::HTTP.new(a).proxy?.should == true
regex = /$#{ENV_JAVA["http.nonProxyHosts"]}/ #dollar needed to behave as expected
if a.hostname.match(regex)
   ENV_JAVA["http.proxyHost"]=nil
end
Net::HTTP.new(a).proxy?.should == false

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文