我编写了以下代码,以获取受基本身份验证
保护的服务的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
提前感谢。
答案 0 :(得分:0)
连接中的问题
即
String basicAuth = "Basic " + Base64.encodeToString("mail@mail.com:123123".getBytes(), Base64.NO_WRAP);
connection.setRequestProperty("Authorization", "basic " + basicAuth );
无需将“basic”与已编码的字符串(basicAuth)绑定。
尝试使用以下代码进行更改:
String basicAuth = "Basic " + Base64.encodeToString("mail@mail.com:123123".getBytes(), Base64.NO_WRAP);
connection.setRequestProperty("Authorization",basicAuth);
我希望它有效。