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

rbb*_*ran 12 java linux settings proxy

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

wik*_*000 19

不要忘记shell变量_JAVA_OPTIONS

export _JAVA_OPTIONS='-Dhttp.proxyHost=cache.com -Dhttp.proxyPort=3128'
Run Code Online (Sandbox Code Playgroud)

有关更多属性,请访问:http: //mindprod.com/jgloss/properties.html

  • 不要忘记[HTTPS有自己的属性](https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html)Roedy没有提到. (2认同)

EGH*_*GHM 6

使用当前的JVM,您可以使用Java属性传递代理主机和端口

java -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080 -Dhttp.noProxyHosts=”localhost|host.mydomain.com” GetURL
Run Code Online (Sandbox Code Playgroud)

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

  • 用户名和密码示例http://www.rgagnon.com/javadetails/java-0085.html http://www.developer.com/java/other/article.php/1551421/Questions-on-HttpURLConnection-and-Proxies热媒 (2认同)

Ker*_*rim 6

您可以使用此脚本将自动环境传递给java应用程序

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

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

这是一个智能连接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
Run Code Online (Sandbox Code Playgroud)