web-dev-qa-db-ja.com

Java Linuxでhttp_proxy変数を使用するプロキシの背後にあるアプリ

XMLファイルをダウンロードするためにインターネットに接続する単純なJavaアプリケーション(コマンドライン))を考えています。問題は、Ubuntuがプロキシを使用してユーザー名とパスワードでインターネットに接続していることです。 (http_proxy ="http://<username>:<pwd>@<ip>:<port>"を介して)。だから私の質問は、プログラムでhttpプロキシとホストを設定する代わりにJavaアプリを書いてhttp_proxy変数を使用することは可能でしょうか?私が書くすべてのアプリ。

13
rbbeltran

シェル変数_Java_OPTIONSを忘れないでください

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

その他のプロパティについては、こちらをご覧ください: http://mindprod.com/jgloss/properties.html

20
wiki1000

このスクリプトを使用して、Javaアプリケーションに渡す環境を自動化できます

これはインテリジェントなスクリプトです。nmapセクションを有効にすると、プロキシのアップまたはダウンステータスが検出されます。アップステータスの場合はプロキシを使用し、ダウンステータスの場合は直接接続を使用します。

このスクリプトを使用すると、アプリを環境設定に接続したり、環境を上書きしたり、プロキシサービスアップ検出方法を使用したりできます。アプリケーションは直接モードまたはプロキシモードを選択します。

これはインテリジェントな接続bashシェルスクリプトです

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
7
Kerim

現在の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 を参照してください。

6
EGHM

ユーザー名とパスワードについては、どうですか?

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