Java - HttpURLConnection setAuthenticator() Method
The Java HttpURLConnection setAuthenticator() method allows to supply an Authenticator to be used when authentication is requested through the HTTP protocol for this HttpURLConnection. If no authenticator is supplied, the default authenticator will be used.
Declaration
Following is the declaration for java.net.HttpURLConnection.setAuthenticator() method
public void setAuthenticator(Authenticator auth)
Parameters
auth − The Authenticator that should be used by this HttpURLConnection.
Return Value
NA
Exception
UnsupportedOperationException −if setting an Authenticator is not supported by the underlying implementation.
IllegalStateException −if URLConnection is already connected.
NullPointerException −if the supplied auth is null.
Example 1
The following example shows the usage of Java HttpURLConnection setAuthenticator() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using setAuthenticator(), we're setting a default authenticator. As default authenticator is null, code will throw a null pointer exception as shown below −
package com.tutorialspoint;
import java.io.IOException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(1000);
urlConnection.setAuthenticator(Authenticator.getDefault());
urlConnection.connect();
System.out.println("Connected.");
urlConnection.disconnect();
System.out.println("Disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:233)
at java.base/sun.net.www.protocol.http.HttpURLConnection.setAuthenticator(HttpURLConnection.java:541)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.setAuthenticator(HttpsURLConnectionImpl.java:503)
at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:14)
Example 2
The following example shows the usage of Java HttpURLConnection setAuthenticator() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using setAuthenticator(), we're setting a null. As authenticator is null, code will throw a null pointer exception as shown below −
package com.tutorialspoint;
import java.io.IOException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(1000);
urlConnection.setAuthenticator(null);
urlConnection.connect();
System.out.println("Connected.");
urlConnection.disconnect();
System.out.println("Disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:233)
at java.base/sun.net.www.protocol.http.HttpURLConnection.setAuthenticator(HttpURLConnection.java:541)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.setAuthenticator(HttpsURLConnectionImpl.java:503)
at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:14)