Java - URL openConnection(Proxy proxy) Method
Description
The Java URL openConnection() method returns a URLConnection instance representing a connection to the remote object referred to by the URL similar to openConnection(), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxing will ignore the proxy parameter and make a normal connection. Invoking this method preempts the system's default ProxySelector settings.
Declaration
Following is the declaration for java.net.URL.openConnection() method
public URLConnection openConnection(Proxy proxy)
Parameters
NA
Return Value
a URLConnection linking to the URL.
Exception
IOException − if an I/O exception occurs.
SecurityException − if a security manager is present and the caller doesn't have permission to connect to the proxy.
IllegalArgumentException − will be thrown if proxy is null, or proxy has the wrong type.
UnsupportedOperationException − if the subclass that implements the protocol handler doesn't support this method.
Example 1
The following example shows the usage of Java URL openConnection(Proxy proxy) method for a valid url with https protocol. In this example, we're creating an instance of URL class. Now using openConnection() method, we're getting the URLConnection object. As next, we're using URLConnection.getInputStream() to get the content of the page of the website pointed by the url and printing the contents of the same −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com");
URLConnection urlConnection = url.openConnection(Proxy.NO_PROXY);
HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
}else {
System.out.println("Please enter an HTTP URL.");
return;
}
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
<!DOCTYPE html><html lang="en"><head><title>Online Tutorials, Courses,...</body></html>
Example 2
The following example shows the usage of Java URL openConnection(Proxy) method for a valid url. In this example, we're creating an instance of URL class with http protocol. Now using openConnection() method, we're getting the URLConnection object. As next, we're using URLConnection.getInputStream() to get the content of the page of the website pointed by the url and printing the contents of the same −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("http","www.tutorialspoint.com","/index.htm");
URLConnection urlConnection = url.openConnection(Proxy.NO_PROXY);
HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
}else {
System.out.println("Please enter an HTTP URL.");
return;
}
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
<!DOCTYPE html><html lang="en"><head><title>Online Tutorials, Courses,...</body></html>
Example 3
The following example shows the usage of Java URL openConnection(Proxy) method for a valid url with file protocol. In this example, we're creating an instance of URL class. Now using openConnection() method, we're getting the URLConnection object. As next, we're using URLConnection.getInputStream() to get the content of the page of the website pointed by the url. As protocol is not http or https, program will print a message Please enter an HTTP URL. and terminates as evident below −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("file","www.tutorialspoint.com","/index.htm");
URLConnection urlConnection = url.openConnection(Proxy.NO_PROXY);
HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
}else {
System.out.println("Please enter an HTTP URL.");
return;
}
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Please enter an HTTP URL.