代理背后的 Java 应用程序在 Linux 中使用 http_proxy 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2430142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java app behind proxy to use http_proxy variable in linux
提问by rbbeltran
I am thinking of a simple Java appplication (command line) that connects to the internet to download a XML file, the problem is that my Ubuntu is using a proxy to connect to internet with username and password (through http_proxy ="http://<username>:<pwd>@<ip>:<port>"
). So my question is, could it be possible to write a java app to use http_proxy
variable? Instead of programmatically setting http proxy and host in every app I will write.
我正在考虑一个连接到互联网以下载 XML 文件的简单 Java 应用程序(命令行),问题是我的 Ubuntu 正在使用代理通过用户名和密码(通过http_proxy ="http://<username>:<pwd>@<ip>:<port>"
)连接到互联网。所以我的问题是,是否可以编写一个 Java 应用程序来使用http_proxy
变量?而不是在我将编写的每个应用程序中以编程方式设置 http 代理和主机。
回答by EGHM
With a current JVM you can pass the proxy host and port using Java properties
使用当前的 JVM,您可以使用 Java 属性传递代理主机和端口
java -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080 -Dhttp.noProxyHosts=”localhost|host.mydomain.com” GetURL
See http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html
请参阅http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html
回答by rbbeltran
in http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.htmlthere is no command to pass proxy username and password to JVM.
在http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html 中,没有将代理用户名和密码传递给 JVM 的命令。
回答by Stephane
For username and password, what about:
对于用户名和密码,怎么样:
-Dhttp.proxyUser=username -Dhttp.proxyPassword=supersecret
回答by wiki1000
Don't forget the shell variable _JAVA_OPTIONS
不要忘记shell变量_JAVA_OPTIONS
export _JAVA_OPTIONS='-Dhttp.proxyHost=cache.com -Dhttp.proxyPort=3128'
For more properties look here: http://mindprod.com/jgloss/properties.html
有关更多属性,请查看此处:http: //mindprod.com/jgloss/properties.html
回答by Kerim
You can use this script to automatic enviroment passing to java application
您可以使用此脚本自动将环境传递给 java 应用程序
This is an intelligent script, if you enable nmap section, it is detecting proxy up or down status , if it is up status it is using proxy if it is down status it is using direct connection..
这是一个智能脚本,如果您启用 nmap 部分,它正在检测代理启动或关闭状态,如果处于启动状态则使用代理如果处于关闭状态则使用直接连接..
With this script you can connect your app with enviroment settings or overwrite enviroment or with proxy service up detection method , the application selects direct or proxy mode
使用此脚本,您可以使用环境设置或覆盖环境或代理服务检测方法连接您的应用程序,应用程序选择直接或代理模式
This is an intelligent connection bash shell script
这是一个智能连接bash shell脚本
Ofcouse if you don't enable nmap service up/down section, this is an simple proxy enviroment or your overwrite value for your application
当然,如果您不启用 nmap 服务上/下部分,这是一个简单的代理环境或您的应用程序的覆盖值
It is producing automaticly proxy connection command line then running your java application
它会自动生成代理连接命令行,然后运行您的 Java 应用程序
This is script's code:
这是脚本的代码:
#!/bin/bash
# Author : Kerim BASOL
# Twitter : http://twitter.com/kerimbasol
# URL : http://kerimbasol.com
# Version : 0.1
# Java Proxy support script
# You can use with GNU License
# Which is your runtime jar file
# Please change this as your application's needs
JARFILE="myapp.jar"
#Automaticly import system proxy settings
if [ -n "$http_proxy" ] ; then
echo $http_proxy | grep "@"
if [ $? -eq 0 ]; then # If variable has username and password, its parse method different
PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/.*@\(.*\):.*//')
PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*@.*:\(.*\)//' | tr -d "/")
USERNAME=$(echo $http_proxy | sed 's/http:\/\/\(.*\)@.*//'|awk -F: '{print }')
PASSWORD=$(echo $http_proxy | sed 's/http:\/\/\(.*\)@.*//'|awk -F: '{print }')
else # If it doesn't have username and password, its parse method this
PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/\(.*\):.*//')
PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*:\(.*\)//' | tr -d "/")
fi
fi
# If you want to overwrite system proxy settings
# uncomment these lines as your wish
#PROXY_HOST="127.0.0.1"
#PROXY_PORT="3128"
#USERNAME="kerimbasol"
#PASSWORD="deneme"
# Display usage
if [ $# -gt 0 ] ; then
if [ = "--help" ] ; then
echo "##代码## [<proxy-server> <proxy-port> [<username> <password> ] ] "
exit 0
fi
fi
# Command line proxy pass
if [ $# -gt 1 ] ; then
PROXY_HOST=
PROXY_PORT=
if [ $# -gt 3 ] ; then
USERNAME=
PASSWORD=
fi
fi
# If you want to use this feature , enables and disables proxy support for proxy service up or down status
# uncomment these line, if you installed nmap
# at ubuntu system you can type this command for this future
# sudo apt-get install nmap
#STATUS=$(nmap -sT $PROXY_HOST -p $PROXY_PORT 2>/dev/null| grep open |awk '{print }')
#if [ "$STATUS" != "open" ]; then # If service isn't running, disable proxy support
# PROXY_HOST=""
# PROXY_PORT=""
#fi
CMD="java -cp."
if [ -n "$PROXY_HOST" -a -n "$PROXY_PORT" ] ; then
CMD="java -cp . -Dhttp.proxyHost=$PROXY_HOST -Dhttp.proxyPort=$PROXY_PORT"
if [ -n "$USERNAME" -a -n "$PASSWORD" ]; then
CMD="$CMD -Dhttp.proxyUser=$USERNAME -Dhttp.proxyPassword=$PASSWORD"
fi
fi
# If you want , change this line as your application wish ;)
CMD="$CMD -jar $JARFILE"
eval $CMD