0

I have used Code igniter REST api on server side. I have set the digest authentication for REST API. I have used volley library for making the http request at client(android) side and using the jsonObjectRequest to call the URL. So if anyone knows how to put the headers for digest authentication in volley request then please help me in this.

Shashikant
  • 21
  • 2
  • 8
  • Please check http://stackoverflow.com/questions/17049473/how-to-set-custom-header-in-volley-request – trebron Mar 18 '16 at 20:55
  • @trebron Thank you..but the solutions provided in the given link are not helpful for me..Do you want any other details about the issue i am facing?..so that you can help me in solving this... – Shashikant Mar 18 '16 at 21:04
  • If you want to send custom headers you have to override getHeaders() in your JsonObjectRequest. Please provide some source code so I can make further clarifications. – trebron Mar 18 '16 at 21:11

2 Answers2

0
JSONObject paramsVolley = new JSONObject();
try {
   paramsVolley.put("u_id", 4);
} catch (JSONException e1) {
   e1.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,      "http://admin:1234@greentin.com/apiuser/getuserdet/format/json", paramsVolley, new Response.Listener<JSONObject>() {
   @Override
   public void onResponse(JSONObject response) {
       Toast.makeText(getApplication(), response.toString(),    Toast.LENGTH_SHORT).show();
   }
   @Override
   public Map<String, String> getHeaders() throws AuthFailureError {
      Map<String, String> headers = new HashMap<>();
      String credentials = "admin:1234";
      String auth = "Digest " + Base64.encodeToString(credentials.getBytes(),    Base64.NO_WRAP);
      headers.put("Content-Type", "application/json");
      headers.put("Authorization", auth);
      return headers;
   }
   }, new Response.ErrorListener() {
   @Override
   public void onErrorResponse(VolleyError e) {
       Toast.makeText(getApplicationContext(), "Error Occurred",Toast.LENGTH_SHORT).show();
   }
});
GoGreen.getInstance().addToRequestQueue(jsonObjReq, tag_update_det);
Shashikant
  • 21
  • 2
  • 8
  • @trebron..this is my code..URL is this http://greentin.com/apiuser/getuserdet/format/json..i forgot to remove the admin:1234 from url... – Shashikant Mar 18 '16 at 21:30
  • And what kind of VolleyError do you receive? – trebron Mar 18 '16 at 21:34
  • If I remove the digest authentication from the server then server gives the proper response – Shashikant Mar 18 '16 at 21:36
  • Base64 encoded user:password looks like Basic authentication, not Digest. For full digest authentication implementation you should look at something like http://www.java2s.com/Open-Source/Android_Free_Code/Framework/platform/com_gm_android_volleyHttpDigestStack_java.htm If Basic authentication is enough for you, just change Digest to Basic in header and make sure that your server handles basic authentication correctly. – trebron Mar 19 '16 at 16:23
  • @trebron so.. is there any other way for DIGEST authentication? – Shashikant Mar 19 '16 at 16:26
  • I've edited my comment - look at implementation of HttpDigestStack and also for last answer in http://stackoverflow.com/questions/34305060/how-to-implement-digest-authentication-using-volley for usage. – trebron Mar 19 '16 at 16:31
  • Do you know how to use that HttpDigestStack class..yesterday I tried to use this..but got fail... – Shashikant Mar 19 '16 at 16:35
0

I have never checked those HttpDigestStack implementation but I will probably start with something like this:

final String userName = "user";
final String password = "password";

// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

DigestAuthenticator auth = new DigestAuthenticator() {
            @Override
            protected PasswordAuthentication requestPasswordAuthentication(String rHost, InetAddress rAddr, int rPort, String rProtocol, String realm, String scheme, URL rURL, Authenticator.RequestorType reqType) {
                return new PasswordAuthentication(userName, password.toCharArray());
            }
        };

HurlStack.UrlRewriter urlRewriter = new HurlStack.UrlRewriter() {

  String rewriteUrl(String url) {
    return url;
  }
};

HttpDigestStack digestStack = new HttpDigestStack(auth, urlRewriter);

// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(digestStack);

// Instantiate the RequestQueue with the cache and network.
RequestQueue mRequestQueue = new RequestQueue(cache, network);
trebron
  • 356
  • 1
  • 4