简体   繁体   English

为 Java 应用程序使用代理服务器

[英]Using Proxy server for Java application

So i have a java app that uses Google Analytics API to gather some info.所以我有一个 Java 应用程序,它使用 Google Analytics API 来收集一些信息。 I am putting this application to run in my oracle cloud managed server which has a firewall and blocks any web calls to work.我将此应用程序在我的 oracle 云托管服务器中运行,该服务器具有防火墙并阻止任何网络调用工作。 So, they setup a proxy for me to use....I've never set up a proxy to work with a java application before, I've been reading at tutorials like this:http://docs.oracle.com/javase/1.5.0/docs/guide/net/proxies.html因此,他们为我设置了一个代理供我使用....我以前从未设置过代理来使用 Java 应用程序,我一直在阅读这样的教程:http ://docs.oracle.com/ javase/1.5.0/docs/guide/net/proxies.html

And i have no idea how to set this up...anyone want to point me in the right direction?而且我不知道如何设置……有人想指出我正确的方向吗?

You must tell your application that there's a proxy somewhere.您必须告诉您的应用程序某处有代理。

As the documentation says, you must set some properties in your virtual machine.正如文档所说,您必须在虚拟机中设置一些属性。 You can do it programatically:您可以以编程方式执行此操作:

//Set the http proxy to webcache.mydomain.com:8080
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setPropery("http.proxyPort", "8080");

// Next connection will be through proxy.
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();

// Now, let's 'unset' the proxy.
System.clearProperty("http.proxyHost");

// From now on http connections will be done directly.

Or use https.proxy... if the connection is HTTPS.或者使用 https.proxy... 如果连接是 HTTPS。 Besides, if you have access to the application server start script, you could add those properties as VM properties with -Dhttp.proxyHost....此外,如果您有权访问应用程序服务器启动脚本,则可以使用 -Dhttp.proxyHost... 将这些属性添加为 VM 属性。

The solution in my case was to configure the JVM with a HTTPS proxy:我的解决方案是使用 HTTPS 代理配置 JVM:

System.setProperty("https.proxyHost", "proxy");
System.setProperty("https.proxyPort", "3128");

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

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