Java Find Proxy Settings

Chapter: Networking Last Updated: 10-09-2016 05:32:12 UTC

Program:

            /* ............... START ............... */
                
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;

public class JavaProxySetting {

	public static void main(String s[]) throws Exception {
		try {
			Properties systemSettings = System.getProperties();
			systemSettings.put("proxySet", "true");
			systemSettings.put("http.proxyHost", "proxy.mycompany1.local");
			systemSettings.put("http.proxyPort", "80");
			URL u = new URL("http://www.google.com");
			HttpURLConnection con = (HttpURLConnection) u.openConnection();
			System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
			System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(false);
		}
		System.setProperty("java.net.useSystemProxies", "true");
		Proxy proxy = (Proxy) ProxySelector.getDefault().select(new URI("http://www.yahoo.com/")).iterator().next();
		;
		System.out.println("proxy hostname : " + proxy.type());
		InetSocketAddress addr = (InetSocketAddress) proxy.address();
		if (addr == null) {
			System.out.println("No Proxy");
		} else {
			System.out.println("proxy hostname : " + addr.getHostName());
			System.out.println("proxy port : " + addr.getPort());
		}
	}
}
                /* ............... END ............... */
        

Output

200 : OK
true
proxy hostname : HTTP
proxy hostname : proxy.mycompany1.local
proxy port : 80

Tags

Proxy Settings, Java, Networking

Similar Programs Chapter Last Updated
Java URL Parts Example Networking 28-10-2016
Java Program To Create Socket At Specific Port Networking 28-10-2016
Java Download Webpage Example Networking 09-09-2016
Java URL Connection Date Networking 09-09-2016
Java Socket To Single Client Networking 09-09-2016
Java Server File Size Example Networking 09-09-2016
Java Multithreaded Server Example Networking 09-09-2016
Java Server File Modification Time Networking 09-09-2016
Java Connect To Server Example Networking 09-09-2016
Http Response Header In Java Example Networking 21-06-2016
Server Socket In Java Example Networking 30-04-2016
Socket Programming In Java Networking 30-04-2016
IP Address And HostName Of Local Computer In Java Networking 30-04-2016
HostName And IP Address In Java Networking 30-04-2016
Client Socket In Java Example Networking 30-04-2016
Read And Download Webpage In Java Example Networking 29-04-2016
Date Of URL Connection In Java Example Networking 29-04-2016
Who Is Lookup In Java Example Networking 21-07-2016
System Proxy In Java Example Networking 21-09-2018
Port Scanner In Java Example Networking 21-07-2016
Ping IP Address In Java Example Networking 21-09-2018
MAC Address In Java Example Networking 22-09-2018
Get IP Address In Java Example Networking 07-04-2016
URL Properties In Java Example Networking 09-09-2016
Default URL Object In Java Example Networking 18-07-2016

1