BHuelse
BHuelse

Reputation: 2981

HttpURLConnection url.openStream() java.io.IOException: No authentication challenges found

an I am trying to load an image from my Server with Android and REST. But when I try to open the stream it crashes with this exception:

myapp: W/System.err? java.io.IOException: No authentication challenges found
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427)
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407)
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356)
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
myapp: W/System.err? at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
myapp: W/System.err? at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
myapp: W/System.err? at java.net.URL.openStream(URL.java:462)
myapp: W/System.err? at de.facentis.GenerstoAndroidTester.Rest.asynctasks.TheOneAndOnlyAsync.getStream(TheOneAndOnlyAsync.java:199)
myapp: W/System.err? at de.facentis.GenerstoAndroidTester.Rest.asynctasks.TheOneAndOnlyAsync.doInBackground(TheOneAndOnlyAsync.java:74)
myapp: W/System.err? at de.facentis.GenerstoAndroidTester.Rest.asynctasks.TheOneAndOnlyAsync.doInBackground(TheOneAndOnlyAsync.java:22)
myapp: W/System.err? at android.os.AsyncTask$2.call(AsyncTask.java:287)
myapp: W/System.err? at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
myapp: W/System.err? at java.util.concurrent.FutureTask.run(FutureTask.java:137)
myapp: W/System.err? at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
myapp: W/System.err? at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
myapp: W/System.err? at java.lang.Thread.run(Thread.java:856)

I found in other threads, here on stackoverflow, it is maybe a 401 from server. I check it and my server sends me a 200 statuscode. The method from server works correctly on other clients.

Here is the method I use:

try {
    InputStream input = null;
    OutputStream output = null;

    try {
        URL url = new URL(methodUri);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();

        connection.setRequestProperty("Authorization", basicHeader);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage());
        connection.setConnectTimeout(5000);
        connection.connect();

        input = new BufferedInputStream(url.openStream());      // <--- Crash
        output = new FileOutputStream(context.getFileStreamPath(filename));

        byte data[] = new byte[1024];
        int count;
        while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
        }

        return;
    } catch (java.net.SocketTimeoutException ex) {
        ex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }finally{
        try {
            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}catch (Exception ex) {
    ex.printStackTrace();
}
return;

Someone na idea?

best regards

Upvotes: 1

Views: 1622

Answers (1)

BHuelse
BHuelse

Reputation: 2981

I found a solution, but dont know why it works. If someone can explain why it works, you are welcome.

I switch from:

input = new BufferedInputStream(url.openStream());

to:

input = connection.getInputStream();

best regards

Upvotes: 1

Related Questions