【问题标题】:Java app behind proxy to use http_proxy variable in linux代理后面的 Java 应用程序在 linux 中使用 http_proxy 变量
【发布时间】:2011-01-26 16:08:52
【问题描述】:

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

【问题讨论】:

    标签: java linux proxy settings


    【解决方案1】:

    使用当前的 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】:

    http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html 中没有将代理用户名和密码传递给JVM 的命令。

    【讨论】:

      【解决方案3】:

      用户名和密码呢:

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

      【讨论】:

        【解决方案4】:

        不要忘记 shell 变量 _JAVA_OPTIONS

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

        更多属性请看这里: http://mindprod.com/jgloss/properties.html

        【讨论】:

        • 别忘了 Roedy 没有提到的 HTTPS has its own properties
        • 就像已经提到的那样:http 和 https 属性:export _JAVA_OPTIONS='-Dhttp.proxyHost=cache.com -Dhttp.proxyPort=3128'export _JAVA_OPTIONS='-Dhttps.proxyHost=cache.com -Dhttps.proxyPort=3128'
        【解决方案5】:

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

        这是一个智能脚本,如果你启用nmap部分,它会检测代理启动或关闭状态,如果它是启动状态它是使用代理如果它是关闭状态它是使用直接连接..

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

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

        如果您不启用 nmap 服务 up/down 部分,这是一个简单的代理环境或您的应用程序的覆盖值

        它会自动生成代理连接命令行,然后运行您的 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-22
          • 1970-01-01
          • 2023-03-08
          • 1970-01-01
          相关资源
          最近更新 更多