Java app behind proxy to use http_proxy variable i

2020-05-19 07:26发布

问题:

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.

回答1:

Don't forget the shell variable _JAVA_OPTIONS

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

For more properties look here: http://mindprod.com/jgloss/properties.html



回答2:

You can use this script to automatic enviroment passing to java application

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

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

Ofcouse if you don't enable nmap service up/down section, this is an simple proxy enviroment or your overwrite value for your application

It is producing automaticly proxy connection command line then running your java application

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:\/\/.*@\(.*\):.*/\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:

With a current JVM you can pass the proxy host and port using Java properties

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



回答4:

For username and password, what about:

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


回答5:

in http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html there is no command to pass proxy username and password to JVM.