Apache Http Digest Authentication using Java -


i working on java project , can't http digest authentication working. tried using apache website, didn't help. have site requires http digest authentication.

        defaulthttpclient httpclient = new defaulthttpclient();         string hosturl = "http://somewebsite.com";         string posturl = "http://somewebsite.com/request";         httppost httppost = new httppost(posturl);         string username = "hello";         string password = "world";         httphost targethost = new httphost(hosturl);          httpclient.getcredentialsprovider().setcredentials(                 new authscope(hosturl, authscope.any_port),                 new usernamepasswordcredentials(username, password));          authcache authcache = new basicauthcache();          digestscheme digestauth = new digestscheme();          digestauth.overrideparamter("realm", "some realm");          digestauth.overrideparamter("nonce", "whatever");         authcache.put(targethost, digestauth);          basichttpcontext localcontext = new basichttpcontext();         localcontext.setattribute(clientcontext.auth_cache, authcache);          // list<namevaluepair> nvps = new arraylist<namevaluepair>();         // nvps.add(new basicnamevaluepair("username", "shirwa99@gmail.com"));         // nvps.add(new basicnamevaluepair("password", "example"));         // httppost.setentity(new urlencodedformentity(nvps));         httpresponse response2 = httpclient.execute(httppost); 

this code works me pretty well:

protected static void downloaddigest(url url, fileoutputstream fos)   throws ioexception {   httphost targethost = new httphost(url.gethost(), url.getport(), url.getprotocol());   closeablehttpclient httpclient = httpclients.createdefault();   httpclientcontext context = httpclientcontext.create();    string credential = url.getuserinfo();   if (credential != null) {     string user = credential.split(":")[0];     string password = credential.split(":")[1];      credentialsprovider credsprovider = new basiccredentialsprovider();     credsprovider.setcredentials(authscope.any,       new usernamepasswordcredentials(user, password));     authcache authcache = new basicauthcache();     digestscheme digestscheme = new digestscheme();     authcache.put(targethost, digestscheme);      context.setcredentialsprovider(credsprovider);     context.setauthcache(authcache);   }    httpget httpget = new httpget(url.getpath());    closeablehttpresponse response = httpclient.execute(targethost, httpget, context);    try {     readablebytechannel rbc = channels.newchannel(response.getentity().getcontent());     fos.getchannel().transferfrom(rbc, 0, long.max_value);   } {     response.close();   } } 

Comments