The point of addiction when performing basic authentication with Java URLConnection

Basic authentication can be accessed with the following URL if the server side supports it (the URL below is a dummy for articles).

http://username:[email protected]

If you hit it with curl as below, you will get the expected result.

$ curl http://username:[email protected]

Doing this with a Java URLConnection will fail.

URL url = new URL("http://username:[email protected]");
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();  //Fail here

On Android, I get the following exception.

java.io.FileNotFoundException: http://username:[email protected]
     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
     at com.example.ogata.Test$1.run(Test.java:53)

In Java, I get the following exception:

java.io.IOException: Server returned HTTP response code: 401 for URL: http://username:[email protected]
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
	at Test$1.run(Test.java:31)

In Java, basic authentication using URLConnection is performed as follows.

URL url = new URL("http://example.com");
URLConnection conn = url.openConnection();
conn.setRequestProperty("Authorization", "Basic " + Base64.encodeToString("username:password".getBytes(), Base64.NO_WRAP));
// conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("username:password".getBytes()));
InputStream is = conn.getInputStream();

The Base64 API is slightly different for Android and Java 8. The commented out line above is Java 8.

Recommended Posts

The point of addiction when performing basic authentication with Java URLConnection
Basic Authentication with Java 11 HttpClient
I tried to make Basic authentication with Java
Calculate the similarity score of strings with JAVA
CI the architecture of Java / Kotlin applications with ArchUnit
Monitor the internal state of Java programs with Kubernetes
Check the behavior of Java Intrinsic Locks with bpftrace
[Java] When writing the source ... A memorandum of understanding ①
The story of making dto, dao-like with java, sqlite
Replace only part of the URL host with java
Summary of how to use the proxy set in IE when connecting with Java
[Java] Simplify the implementation of data history management with Reladomo
Count the number of digits after the decimal point in Java
Specify the character code of the source when building with Maven
About the behavior when doing a file map with java
Be sure to compare the result of Java compareTo with 0
Read the data of Shizuoka point cloud DB with Java and generate aerial photograph and elevation PNG.
Read the data of Shizuoka point cloud DB with Java and try to detect the tree height.
Handling of java floating point [Note] while reading the reference book
Switch the version of java installed by SDKMAN when moving directories
[Java1.8 +] Get the date of the next x day of the week with LocalDate
Try Hello World with the minimum configuration of Heroku Java spring-boot
Connecting to a database with Java (Part 1) Maybe the basic method
A story about hitting the League Of Legends API with JAVA
Overwrite upload of file with the same name with BOX SDK (java)
Is the version of Elasticsearch you are using compatible with Java 11?
Investigation method when the CPU of the server running java is heavy
[Java] Delete the elements of List
Use Basic Authentication with Spring Boot
Understand the basic mechanism of log4j2.xml
[Java version] The story of serialization
About signature authentication with java 1st
The basic basis of Swift dialogs
Basic usage of java Optional Part 1
[Note] Handling of Java decimal point
Basic processing flow of java Stream
[Basic knowledge of Java] Scope of variables
The origin of Java lambda expressions
Basic structure of Java source code
The story of making a game launcher with automatic loading function [Java]
Let's express the result of analyzing Java bytecode with a class diagram
How to find the total number of pages when paging in Java
Check the domain by checking the MX record of the email address with java
I was stuck with the handling of the time zone when formatting with SimpleDateFormat
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
A description of the JDBC Sample that will be helpful when developing a custom authentication provider with the Cognos SDK