【问题标题】:Why do I get android java.io.IOException: No authentication challenges found?为什么我得到 android java.io.IOException: No authentication challenge found?
【发布时间】:2016-04-20 11:17:11
【问题描述】:

我编写了以下代码从受基本身份验证保护的服务中获取 json 字符串

  InputStream input;
                    StringBuilder sb = new StringBuilder();
                    HttpURLConnection connection;
                    try {
                        URL url = new URL(params[0]);
                        connection = (HttpURLConnection) url.openConnection();
                        String basicAuth = "Basic " + Base64.encodeToString("mail@mail.com:123123".getBytes(), Base64.NO_WRAP);
                        connection.setRequestProperty("Authorization", "basic " + basicAuth );


                        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                            return "server retured" + connection.getResponseCode() + " " + connection.getResponseMessage();
                        }

                        input = connection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"), 8);
                        String line;
                        while ((line = reader.readLine()) != null) {
                          sb.append(line + "\n");
                        }
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return sb.toString();

此函数返回异常表示未找到身份验证挑战

我看到有人说您必须包含 WWW-Authenticate 标头字段

如果是解决方案,如何包含它?或者什么是正确的解决方案?

这是堆栈跟踪

04-20 13:31:19.830 32036-1110/com.example.computer.myapplication W/System.err: java.io.IOException: No authentication challenges found
04-20 13:31:19.930 32036-1110/com.example.computer.myapplication W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getAuthorizationCredentials(HttpURLConnectionImpl.java:427)
04-20 13:31:19.935 32036-1110/com.example.computer.myapplication W/System.err:     at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:407)
04-20 13:31:19.940 32036-1110/com.example.computer.myapplication W/System.err:     at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:356)
04-20 13:31:19.940 32036-1110/com.example.computer.myapplication W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
04-20 13:31:19.940 32036-1110/com.example.computer.myapplication W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
04-20 13:31:19.940 32036-1110/com.example.computer.myapplication W/System.err:     at com.example.computer.myapplication.MainActivity$3.doInBackground(MainActivity.java:264)
04-20 13:31:19.945 32036-1110/com.example.computer.myapplication W/System.err:     at com.example.computer.myapplication.MainActivity$3.doInBackground(MainActivity.java:250)
04-20 13:31:19.945 32036-1110/com.example.computer.myapplication W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-20 13:31:19.945 32036-1110/com.example.computer.myapplication W/System.err:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 13:31:19.950 32036-1110/com.example.computer.myapplication W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 13:31:19.950 32036-1110/com.example.computer.myapplication W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-20 13:31:19.950 32036-1110/com.example.computer.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-20 13:31:19.950 32036-1110/com.example.computer.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-20 13:31:19.955 32036-1110/com.example.computer.myapplication W/System.err:     at java.lang.Thread.run(Thread.java:856)
04-20 13:31:24.735 32036-32037/com.example.computer.myapplication D/dalvikvm: GC_CONCURRENT freed 405K, 7% free 14712K/15751K, paused 170ms+16ms, total 258ms

提前致谢。

【问题讨论】:

  • 请添加堆栈跟踪:)
  • 我添加了堆栈跟踪
  • 我想你的问题在这里得到了回答:stackoverflow.com/questions/17121213/…
  • 我知道这篇文章,但我问我如何才能做到这一点
  • 如何以正确的方式将 WWW-Authenticate 添加到连接中

标签: java android


【解决方案1】:

我在连接中遇到问题

String basicAuth = "Basic " + Base64.encodeToString("mail@mail.com:123123".getBytes(), Base64.NO_WRAP);  
    connection.setRequestProperty("Authorization", "basic " + basicAuth );

无需将“基本”与已编码的字符串 (basicAuth) 绑定。

尝试使用以下代码进行更改:

String basicAuth = "Basic " + Base64.encodeToString("mail@mail.com:123123".getBytes(), Base64.NO_WRAP);  
        connection.setRequestProperty("Authorization",basicAuth);  

我希望它有效。

【讨论】:

  • 哇,问题是基本这个词,谢谢伙计,对不起,我的眼睛不再锐利了
猜你喜欢
  • 2014-08-29
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多