代理后面的Java app在linux中使用http_proxy变量

时间:2010-03-12 02:34:43

标签: java linux proxy settings

我正在考虑一个简单的Java应用程序(命令行)连接到互联网下载XML文件,问题是我的Ubuntu正在使用代理通过用户名和密码连接到互联网(通过{{1} })。所以我的问题是,是否有可能编写一个java应用程序来使用http_proxy ="http://<username>:<pwd>@<ip>:<port>"变量?而不是以编程方式在我将编写的每个应用程序中设置http代理和主机。

5 个答案:

答案 0 :(得分:18)

不要忘记shell变量_JAVA_OPTIONS

export _JAVA_OPTIONS='-Dhttp.proxyHost=cache.com -Dhttp.proxyPort=3128'

有关更多属性,请点击此处: http://mindprod.com/jgloss/properties.html

答案 1 :(得分:6)

使用当前的JVM,您可以使用Java属性

传递代理主机和端口
java -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080 -Dhttp.noProxyHosts=”localhost|host.mydomain.com” GetURL

请参阅http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

答案 2 :(得分:6)

您可以将此脚本用于自动环境传递到Java应用程序

这是一个智能脚本,如果你启用了nmap部分,它会检测代理上升或下降状态,如果它处于up状态,它正在使用代理,如果它处于关闭状态,它正在使用直接连接..

使用此脚本,您可以将您的应用与环境设置或覆盖环境或代理服务检测方法连接,应用程序选择直接或代理模式

这是一个智能连接bash shell脚本

如果你没有启用nmap服务上/下部分,这是一个简单的代理环境或你的应用程序的覆盖值

自动生成代理连接命令行,然后运行java应用程序

这是脚本的代码:

#!/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:\/\/.*@\(.*\):.*/\1/')
    PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*@.*:\(.*\)/\1/' | tr -d "/")
    USERNAME=$(echo $http_proxy | sed 's/http:\/\/\(.*\)@.*/\1/'|awk -F: '{print $1}')
    PASSWORD=$(echo $http_proxy | sed 's/http:\/\/\(.*\)@.*/\1/'|awk -F: '{print $2}')
    else # If it doesn't have username and password, its parse method this
    PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/\(.*\):.*/\1/')
    PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*:\(.*\)/\1/' | 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 [ $1 = "--help" ] ; then
            echo "$0 [<proxy-server> <proxy-port> [<username>  <password> ] ] "
            exit 0
    fi
fi

# Command line proxy pass
if [ $# -gt 1 ] ; then
    PROXY_HOST=$1
    PROXY_PORT=$2
    if [ $# -gt 3 ] ; then
            USERNAME=$3
            PASSWORD=$4
    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 $2}')
#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

答案 3 :(得分:1)

对于用户名和密码,请参阅:

-Dhttp.proxyUser=username -Dhttp.proxyPassword=supersecret

答案 4 :(得分:-3)

http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html中的

没有命令将代理用户名和密码传递给JVM。