java client program to send digest authentication request using HttpClient API

Posted by Rajesh on Stack Overflow See other posts from Stack Overflow or by Rajesh
Published on 2010-01-28T05:48:55Z Indexed on 2010/04/02 3:33 UTC
Read the original article Hit count: 797

Filed under:
|
|

I have restlet sample client program which sends the digest request. Similar to this I need java client program which sends a digest request using HttpClient api. Can anybody send me sample code. Thanks in advance.

        Reference reference = new Reference("http://localhost:8092/authenticate");
        Client client = new Client(Protocol.HTTP);
        Request request = new Request(Method.GET, reference);
        Response response = client.handle(request);
        System.out.println("response: "+response.getStatus());

        Form form = new Form();
        form.add("username", "rajesh");
        form.add("uri", reference.getPath());

        // Loop over the challengeRequest objects sent by the server.
        for (ChallengeRequest challengeRequest : response
                .getChallengeRequests()) {
            // Get the data from the server's response.
            if (ChallengeScheme.HTTP_DIGEST
                    .equals(challengeRequest.getScheme())) {
                Series<Parameter> params = challengeRequest.getParameters();
                form.add(params.getFirst("nonce"));
                form.add(params.getFirst("realm"));
                form.add(params.getFirst("domain"));
                form.add(params.getFirst("algorithm"));
                form.add(params.getFirst("qop"));
            }
        }

        // Compute the required data
        String a1 = Engine.getInstance().toMd5(
                "rajesh" + ":" + form.getFirstValue("realm") + ":" + "rajesh");
        String a2 = Engine.getInstance().toMd5(
                request.getMethod() + ":" + form.getFirstValue("uri"));
        form.add("response", Engine.getInstance().toMd5(
                a1 + ":" + form.getFirstValue("nonce") + ":" + a2));

        ChallengeResponse challengeResponse = new ChallengeResponse(
                ChallengeScheme.HTTP_DIGEST, "", "");
        challengeResponse.setCredentialComponents(form);

        // Send the completed request
        request.setChallengeResponse(challengeResponse);
        response = client.handle(request);

        // Should be 200.
        System.out.println(response.getStatus());

© Stack Overflow or respective owner

Related posts about java

Related posts about authentication