HttpURLConnection worked fine in Android 2.x but NOT in 4.1: No authentication challenges found
I am currently facing the same problem. On 4.1 Jelly Bean I receive an IOException "No authentication challenges found" when calling getResponseCode() on the HttpURLConnection.
I have searched online to see what has changed in the Android source code and found the following:
4.0.4 (working): https://bitbucket.org/seandroid/libcore/src/7ecbe081ec95/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java
4.1.1 (not working): https://bitbucket.org/seandroid/libcore/src/6b27266a2856/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java
As one can see in 4.1 JB the method getAuthorizationCredentials() throws the IOException. It parses the challenge headers it finds in the response using HeaderParser.parseChallenges(..), if the response code is 401 or 407. If the returned List is empty the Exception is thrown.
https://bitbucket.org/seandroid/libcore/src/6b27266a2856/luni/src/main/java/libcore/net/http/HeaderParser.java
We are currently investigating what exactly causes that List to be empty, but have the suspicion that our server might use realm=... instead of realm="..." in the challenge header. Missing quotation marks might be the cause for this problem. We have to investigate further if that is indeed the case and if we can make it work.
android - no authentication challenges found Error with Get request
I think the problem perhaps is setDoOutput()
, I have tested this sample code, you can read my comments
private class APIRequest extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
String token = "0123456789";
URL url = new URL("https://validate.co.nz/api/public/api/authenticate");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true); //HERE: if TRUE, receive "No authentication challenges found"; if FALSE, receive "token_not_provided"
urlConnection.setRequestMethod("GET"); // I think if GET, should setDoOutput(false);
urlConnection.setRequestProperty("Authorization", "Basic " + token);
urlConnection.connect();
InputStream inputStream;
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
inputStream = urlConnection.getErrorStream();
} else {
inputStream = urlConnection.getInputStream();
}
return String.valueOf(urlConnection.getResponseCode()) + " " + urlConnection.getResponseMessage();
} catch (Exception e) {
return e.toString();
}
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
}
}
HTTP Basic Authentication issue on Android Jelly Bean 4.1 using HttpURLConnection
We were able to solve Jelly Bean not calling getPasswordAuthentication() of the Authenticator via the following new method:
@TargetApi(Build.VERSION_CODES.FROYO)
private void setJellyBeanAuth(HttpURLConnection httpConn) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
byte[] auth = (USER + ":" + PASSWORD).getBytes();
String basic = Base64.encodeToString(auth, Base64.NO_WRAP);
httpConn.setRequestProperty("Authorization", "Basic " + basic);
}
}
Then just call this function after opening the connection:
httpURLConn = (HttpURLConnection) url.openConnection();
setJellyBeanAuth(httpURLConn);
The @TargetApi for Froyo annotation is only necessary since we are still supporting API 7 while Base64 was added in API 8
FileNotFoundException for HttpURLConnection in Ice Cream Sandwich
Try removing the setDoOutput call. Taken from this blog:
a blog
Edit: This is needed when using a POST call.
Related Topics
Android - How to Get Application Name? (Not Package Name)
How to Change Text Color of Simple List Item
How to Set the Airplane_Mode_On to "True" or On
Android: Notifydatasetchanged(); Not Working
How to Change the Background of Android Alert Dialogs
Replacing Default Phone App on Android 6 and 7 with Incallservice
How to Read Contacts in Android Using Realm
How to Set the Focus (And Display the Keyboard) on My Edittext Programmatically
Determine When a Viewpager Changes Pages
How to Hide Android Soft Keyboard on Edittext
Applying Colorfilter to Imageview with Shapeddrawable
Kotlin Error:Could Not Find Org.Jetbrains.Kotlin:Kotlin-Stdlib-Jre7:1.0.7
Downloading/Caching Google Maps for Offline Use
Replacing Glreadpixels with Egl_Khr_Image_Base for Faster Pixel Copy
Most Memory Efficient Way to Resize Bitmaps on Android
Nexus 7 Not Visible Over Usb via "Adb Devices" from Windows 7 X64