Volley BasicNetwork.performRequest:正常身份验证时出现意外的响应代码401

时间:2018-12-18 18:31:59

标签: android authentication android-volley response http-status-code-401

使用此代码我总是会收到此401错误,以接受常规身份验证。 我还添加了文件network_security_config.xml来绕过一些其他信息消息:

String http_post() {
RequestQueue MyRequestQueue = Volley.newRequestQueue(Library.this);

//String url = "http://" + "hqplayer" + ":" + Utils.password + "@" + Utils.ip + ":8088/library";
String url = "http://" + Utils.ip + ":8088/library";

Log.i(TAG, "HttpPost() <" + url + ">");

MyRequestQueue.add(new StringRequest(Request.Method.POST, url,

   new response.Listener<String>() {
   @Override
   public void onResponse(String response) {
       Log.i(TAG, "HttpPost() - onResponse() ");
   }},

   new Response.ErrorListener() {
   @Override
   public void onErrorResponse(VolleyError error) {
       Log.i(TAG, "HttpPost() - onErrorResponse() ");
       Log.i(TAG, "HttpPost() error <" + error + ">");
   }}) {

       @Override
       public Map<String, String> getHeaders() throws AuthFailureError {
          HashMap<String, String> params = new HashMap<String, String>();
          String creds = String.format("%s:%s","hqplayer","valvole");
          String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
          params.put("Authorization", auth);
          return params;
     }});
     return null;
}

1 个答案:

答案 0 :(得分:0)

在我的情况下,此代码也不起作用:

@Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            /*
            Map<String, String> headers = new HashMap<>();

            String credentials = "hqplayer:valvole";

            Log.i(TAG, "HttpPost() credentials <" + credentials + ">");

            String auth = "Basic "
                    + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
            headers.put("Content-Type", "application/json");
            headers.put("Authorization", auth);

            Log.i(TAG, "HttpPost() - end() ");

            return headers;

        }