Overview
For local networks within an organization, access to the public-domain Internet is often via a HTTP Proxy. This article talks about the HTTP proxy settings for the Java environment. I did not find a good document on the Web to describe these settings; Had to discover many of them by trial-and-error. Hence this article.
Keywords
HTTP Proxy, Java Proxy Settings, Tomcat, Application Server, Servlets, HTTP Proxy Authentication for Java, Java Application Proxy Settings
Scenario
- Your Java client runs on a machine on the Local network – Private LAN. The client could be a standalone application, or a servlet hosted on a web container like Tomcat
- Your code access an external resource using HTTP. For example, invoking an external Web Service.
- Your HTTP call needs to tunnel through the HTTP proxy (using SOCKS authentication). Even if authentication is not required, you would still need to configure the URL and the Port of your HTTP proxy.
Settings
Use one of the methods below for your JVM proxy settings. Try an alternate method if any particular method does not work. In most cases, you should not require any change the pre-compiled Java code for proxy settings. JVM’s environment settings should be enough to fix this problem.
Command Line JVM Settings
The proxy settings are given to the JVM via command line arguments:
$ java -Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber -Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword javaClassToRun
Setting System Properties in Code
Add the following lines in your Java code so that JVM uses the proxy to make HTTP calls. This would, of course, require you to recompile your Java source. (The other methods do not require any recompilation.):
System.getProperties().put("http.proxyHost", "someProxyURL"); System.getProperties().put("http.proxyPort", "someProxyPort"); System.getProperties().put("http.proxyUser", "someUserName"); System.getProperties().put("http.proxyPassword", "somePassword");
Don’t hardcode the proxy settings in your source. Read these settings from a configurable text file, so your users can configure them. You might also need to set this property:
System.getProperties().put("proxySet", "true");
Or
System.getProperties().put("http.proxySet", "true");
Tomcat Settings: catalina.properties
Append these properties to the catalina.properties file in Tomcat: ${CATALINA_OME}/conf/catalina.properties file:
http.proxyHost=yourProxyURL http.proxyPort=yourProxyPort http.proxyUser=yourUserName http.proxyPassword=yourPassword
Tomcat Settings: catalina.bat
Add all the parameters defined above in the ${CATALINA_HOME}/bin/catalina.bat (for Windows) or ${CATALINA_HOME}/bin/catalina.bat (for *nix):
JAVA_OPTS="-Dhttp.proxyHost=yourProxyURL ..."
(Each option is seperated by spaces.)
References
- HTTP Proxy Configuration for JVM – Developer Life
- Apache Axis Client – Setting JVM Parameters for HTTP Proxy
- Ericsson Documentation – Setting HTTP Proxy for Tomcat
- Using JAVA_OPTS for Proxy Settings
- Confluence – Proxy Support for various containers
- Sun – Java Networking Properties
Good effort, using Proxy class gives us more freedom. You can refer this link Java Networking and Proxies
Hello
Great book. I just want to say what a fantastic thing you are doing! Good luck!
G’night
where to fill the domain? domain/yourUserName is failed
good
Pingback: Aaron Johnson » Blog Archive » Links: 11-13-2007
Pingback: Aaron Johnson » Blog Archive » Java, Commons HTTP Client and HTTP proxies
Pingback: Links: 11-14-2007 :: the correct shade of blue
very interesting.
i’m adding in RSS Reader
Thanks, very useful for me 🙂
so with this piece of code, i could integrate it with an application? and it would automatically go through the proxy? as long as i had access to the http proxy service?
Thanx.. dude…
helped me much….
while working wid amazon ec2 and soap…
Thanks for the info.
Is it possible to specify a .pac script(Proxy Auto Configuration) instead of proxy host information?
Thank you. I was looking for it for a long time!
Imran, does your code above really work if the proxy requires authentication? I don’t think it does, note that http.proxyUser / http.proxyPassword are standard properties like http.proxyHost.
http://java.sun.com/j2se/1.5.0/docs/guide/net/proxies.html
You have to use a java.net.Authenticator, sample here:
http://www.softonaut.com/2008/06/09/using-javanetauthenticator-for-proxy-authentication/
Thanks Jamshid for providing additional details and links.
Tanks!!!
very very useful your information….!!!!
Abraços…
HI
I am getting following error kindly help me
java.io.IOException: Server returned HTTP response code: 407 for URL: http://wwwd.way2sms.com/auth.cl
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1236)
at com.aswinanand.sms.SMS.send(SMS.java:76)
at MeraSMS.main(MeraSMS.java:31)
Hi
I am also getting same problem ,any one kindly help to solve the below issue
java.io.IOException: Server returned HTTP response code: 407 for URL: http://wwwd.way2sms.com/auth.cl
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1236)
at com.aswinanand.sms.SMS.send(SMS.java:76)
at MeraSMS.main(MeraSMS.java:31)
Several errors here.
1. The proxySet property has never existed in the JDK, so setting it to ‘true’ or ‘false’ or ‘fred’ does nothing. It was part of the now very defunct HotJavabean (1997) and leaked into various books.
2. The http.proxyUser and http.proxyPassword properties aren’t part of the JDK either. I believe they are part of one of the Apache libraries.
This is cool man.. Thanks so much!!
should be catalina.sh instead of catalina.bat for *nix.
We have a automatic proxy configuration file “***.pac” file.
There is no user name or password,
Does anyone know how to code for this situation?
Hi I am running a standalone program as proxy server and provided all properties, but it is accepting though the user name is wrong.
/***************************************************************************
* Copyright (C)2011 Deutsche Bank AG APHO
* %created_by: Vijender D (devvije) %
* %derived_by: %
* %date_created: 3:29:17 PM May 12, 2011 %
* %date_modified: %
* %state: %
* %full_filespec: %
* %release: %
*
* Author: Vijender D (devvije)
*
* Created on: May 12, 2011
*
* Purpose:
*
* Change History:
* Date Author Reason
* ————————————————————————–
* May 12, 2011 Vijender D
*
***************************************************************************/
/**
* @author devvije
*
*/
import java.io.*;
import java.net.*;
public class SimpleProxyServer {
public static void main(String[] args) throws IOException {
try {
String localhost = “10.172.85.193”;
String remotehost = “10.171.160.22”;
int remoteport = 6666;
int localport = 6666;
// Print a start-up message
System.out.println(“Starting proxy for ” + localhost + “:” + remoteport
+ ” on port ” + localport);
System.getProperties().put(“http.proxyHost”, localhost);
System.getProperties().put(“http.proxyPort”, localport);
System.getProperties().put(“http.proxyUser”, “vijender”);
System.getProperties().put(“http.proxyPassword”, “vijender123”);
System.getProperties().put(“proxySet”, “true”);
System.getProperties().put(“http.proxySet”, “true”);
// And start running the server
runServer(remotehost, remoteport, localport); // never returns
} catch (Exception e) {
System.err.println(e);
}
}
/**
* runs a single-threaded proxy server on
* the specified local port. It never returns.
*/
public static void runServer(String remotehost, int remoteport, int localport)
throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(localport);
final byte[] request = new byte[1024];
byte[] reply = new byte[4096];
while (true) {
Socket client = null, server = null;
try {
// Wait for a connection on the local port
client = ss.accept();
final InputStream streamFromClient = client.getInputStream();
final OutputStream streamToClient = client.getOutputStream();
// Make a connection to the real server.
// If we cannot connect to the server, send an error to the
// client, disconnect, and continue waiting for connections.
try {
server = new Socket(remotehost, remoteport);
} catch (IOException e) {
PrintWriter out = new PrintWriter(streamToClient);
out.print(“Proxy server cannot connect to ” + remotehost + “:”
+ remoteport + “:\n” + e + “\n”);
out.flush();
client.close();
continue;
}
// Get server streams.
final InputStream streamFromServer = server.getInputStream();
final OutputStream streamToServer = server.getOutputStream();
// a thread to read the client’s requests and pass them
// to the server. A separate thread for asynchronous.
Thread t = new Thread() {
public void run() {
int bytesRead;
try {
while ((bytesRead = streamFromClient.read(request)) != -1) {
streamToServer.write(request, 0, bytesRead);
streamToServer.flush();
}
} catch (IOException e) {
}
// the client closed the connection to us, so close our
// connection to the server.
try {
streamToServer.close();
} catch (IOException e) {
}
}
};
// Start the client-to-server request thread running
t.start();
// Read the server’s responses
// and pass them back to the client.
int bytesRead;
try {
while ((bytesRead = streamFromServer.read(reply)) != -1) {
streamToClient.write(reply, 0, bytesRead);
streamToClient.flush();
}
} catch (IOException e) {
}
// The server closed its connection to us, so we close our
// connection to our client.
streamToClient.close();
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (server != null)
server.close();
if (client != null)
client.close();
} catch (IOException e) {
}
}
}
}
}
Hiya very nice site!! Guy .. Excellent .. Amazing .. I’ll bookmark your site and take the feeds additionally?I’m glad to search out numerous useful information right here within the post, we’d like work out more techniques in this regard, thanks for sharing. . . . . .
Pingback: jvm http proxy – wiloon
There is NO SUCH THING as http.proxyUser and http.proxyPassword! They don’t exist. Proxies with authentication will not work with these commands.
Excellent post. I was checking continuously this blog and I am impressed!
Very helpful info particularly the last part :
) I care for such info a lot. I was seeking this particular information for a long time.
Thank you and good luck.
Reblogged this on Around the Code !!.