How to Configure Network Settings in Java - Stack Abuse

How to Configure Network Settings in Java


Setting the proxy server and port:

System.setProperty("http.proxyHost", "");

System.setProperty("http.proxyPort", "80");

For an HTTPS proxy, just change 'http' to 'https' for each property.

Or you can just use the system's proxies:

System.setProperty("", "true");

IP Protocol

Disabling the IPv6 stack and forcing IPv4:

System.setProperty("" , "true");

Use IPv6 addresses when possible:

System.setProperty("" , "true");


Setting a SOCKS proxy:

System.setProperty("socksProxyHost" , "");

System.setProperty("socksProxyPort" , "1080")

Specify the SOCKS protocol used by the server:

System.setProperty("socksProxyVersion" , "4");

Authenticating with a SOCKSv5 proxy server:

System.setProperty("" , "myusername");

System.setProperty("" , "mypassword");


Setting a TrustStore that was created with keytool:

System.setProperty("", "/path/to/truststore");

Misc. Properties

Set the string sent in the User-Agent request header in HTTP requests:

System.setProperty("http.agent", "foobar");

Keep in mind that this new string will have "Java/" appended at the end. So the example above would yield "foobar Java/1.6.0" if the current Java version being used is 1.6.0.

Prevent persistent connections:

System.setProperty("http.keepalive", "false");

Set the maximum number of idle connections that will be kept alive (http.keepalive must be true):

System.setProperty("http.maxConnections", "5");

Set the maximum number of redirects that will be followed for an HTTP request:

System.setProperty("http.maxRedirects", "20");
Last Updated: December 2nd, 2015

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:

    Git Essentials

    Check out this hands-on, practical guide to learning Git, with best-practices and industry-accepted standards. Stop Googling Git commands and actually learn it!

    © 2013-2021 Stack Abuse. All rights reserved.