Zulfi,

Try setting both realm & host to null. That should do the trick

        HttpClient hc = new HttpClient();
        HttpState state = hc.getState();
        state.setAuthenticationPreemptive(true);
      // Set default credentials (realm & host are null)
        state.setCredentials(null, null, 
            new UsernamePasswordCredentials("zulfi", "zulfi"));


Folks,

The present convention for setting a default set of credentials is utterly confusing 
and needs to be redesigned in 2.1. I believe we should be using 
HttpState#setCredentials(HttpAuthRealm, Credentials) instead of 
HttpState#setCredentials(String, String, Credentials). We should also provide a static 
final class to represent the default set of credentials:

public static final HttpAuthRealm DEFAULT_AUTH_CREDENTIALS = new HttpAuthRealm(null, 
null);

The end user code might look similar to that below

        state.setCredentials(DEFAULT_AUTH_CREDENTIALS, 
            new UsernamePasswordCredentials("zulfi", "zulfi"));


Cheers

Oleg


-----Original Message-----
From: Zulfi Umrani [mailto:[EMAIL PROTECTED]
Sent: Tuesday, June 10, 2003 00:44
To: [EMAIL PROTECTED]
Subject: preemptive


Tried to use the Preemptive Authentication feature. Could not get it to
work. I used the HttpState.setAuthenticationPreemptive(true); to set the
preemptive authentication ON. It still send the first request without
the Authorization header. Code sample is below. Would like to know, how
to set up the Pre-emptive Authentication.

package test;

import java.io.*;
import java.net.URL;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.auth.*;
import org.apache.commons.httpclient.util.*;

public class JCTest {
    public static void main(String[] args) throws Exception {
        test0();
        test0();
        return;
    }

    public static void test0() throws Exception {
        System.out.println("running test0");

        String urlstr = "http://localhost:9999/services1/test";;
        URL url = new URL(urlstr);
        
        HttpClient hc = new HttpClient();
        HttpState state = hc.getState();
        state.setAuthenticationPreemptive(true);
        state.setCredentials("", url.getHost(), 
            new UsernamePasswordCredentials("zulfi", "zulfi"));

        PostMethod post = new PostMethod(urlstr);
        post.setDoAuthentication(true);

        post.addRequestHeader("Connection", "Keep-Alive");
        post.addRequestHeader("Content-Length", ""+msg.length());
        post.addRequestHeader("Content-Type", "text/xml;
charset=utf-8");

        InputStream reqis = new ByteArrayInputStream(msg.getBytes());
        
        post.setRequestBody(reqis);

        HostConfiguration hconfig = new HostConfiguration();
        hconfig.setHost(new URI(urlstr));
        
        hc.executeMethod(hconfig, post);

        System.out.println(post.getResponseBodyAsString());
        System.out.println();
            
    }
    
    private static String msg = "Text Message";

}


Thanks.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to