Oleg, Thanks for the prompt response. I followed your suggestion and it worked. Many thanks to you and others who developed such an excellent product. Can you direct me to where I can donate some money? I'd like to contribute some code in the future as well.
Zhaohua "Oleg Kalnichevski" <[EMAIL PROTECTED]> 08/03/2005 06:08 PM Please respond to "Jakarta Commons Users List" <[email protected]> To "Jakarta Commons Users List" <[email protected]> cc Subject Re: httpclient running in a loop: the code Zhaohua, It is all very simple. The MyCredentialsProvider class of yours always returns the same credentials, which causes HttpClient to produce the same authentication response over and over again. HttpClient makes no provisions to check whether the same credentials have not been tried already. It is the responsibility of the custom CredentialsProvider to ensure that the same credentials are not used multiple times for the same authentication scope (that is, the same scheme, realm, host, and port). I do admit this may not be obvious and the javadocs do not reflect this assumption. I'll make sure the javasocs get updated If you not intend to prompt the user for a new set of credentials [1], or retrieve it from elsewhere, you should simply add the credentials to the HTTP state and HttpClient will make sure the same credentials are not tried multiple times. httpclient.getState().setCredentials( new AuthScope("driman8.cgsh.com", 80, "cgsh.com"), new UsernamePasswordCredentials("myusername", "mypassword")); Hope this helps Oleg [1] http://svn.apache.org/viewcvs.cgi/jakarta/commons/proper/httpclient/trunk/src/examples/InteractiveAuthenticationExample.java?view=markup On Wed, 2005-08-03 at 15:36 -0400, Zhaohua Meng wrote: > Sorry I have to split this mail in 2 since apache.org mail server wont' > allow large messages. > The code: > > import java.io.File; > import java.io.FileInputStream; > import java.io.IOException; > import java.security.Security; > import java.util.ArrayList; > import java.util.List; > > import javax.xml.parsers.ParserConfigurationException; > > import org.apache.commons.httpclient.Credentials; > import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; > import org.apache.commons.httpclient.Header; > import org.apache.commons.httpclient.HttpClient; > import org.apache.commons.httpclient.HttpException; > import org.apache.commons.httpclient.HttpVersion; > import org.apache.commons.httpclient.NTCredentials; > import org.apache.commons.httpclient.UsernamePasswordCredentials; > import org.apache.commons.httpclient.auth.AuthPolicy; > import org.apache.commons.httpclient.auth.AuthScheme; > import org.apache.commons.httpclient.auth.AuthScope; > import > org.apache.commons.httpclient.auth.CredentialsNotAvailableException; > import org.apache.commons.httpclient.auth.CredentialsProvider; > import org.apache.commons.httpclient.cookie.CookiePolicy; > import org.apache.commons.httpclient.methods.GetMethod; > import org.apache.commons.httpclient.methods.InputStreamRequestEntity; > import org.apache.commons.httpclient.methods.PostMethod; > import org.apache.commons.httpclient.methods.StringRequestEntity; > import org.apache.commons.httpclient.params.HttpMethodParams; > > /** > * > * This is a sample application that demonstrates > * how to use the Jakarta HttpClient API. > * > * This application sends an XML document > * to a remote web server using HTTP POST > * > * @author Sean C. Sullivan > * @author Ortwin Glück > * @author Oleg Kalnichevski > */ > public class PostXML { > private Object password; > > /** > * > * Usage: > * java PostXML http://mywebserver:80/ c:\foo.xml > * > * @param args command line arguments > * Argument 0 is a URL to a web server > * Argument 1 is a local filename > * > */ > public static void main(String[] args) throws Exception { > System.setProperty("org.apache.commons.logging.Log", > "org.apache.commons.logging.impl.SimpleLog"); > System.setProperty( > "org.apache.commons.logging.simplelog.showdatetime", "true"); > System.setProperty( > "org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); > System.setProperty( > "org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", > "debug"); > postWithSupportedAuth(); > } > public static void postWithSupportedAuth() throws IOException, > HttpException, IllegalAccessException, InstantiationException, > ClassNotFoundException, ParserConfigurationException { > String secProviderName = "com.sun.crypto.provider.SunJCE"; > java.security.Provider secProvider = > (java.security.Provider)Class.forName(secProviderName).newInstance(); > Security.addProvider(secProvider); > String strURL = " > http://driman8.cgsh.com/worksite/services/factory.asmx"; > String strXMLFilename = "C:/project/junk/Test/Java > Source/request.xml"; > String requestDoc = > ImanageCreateWorkspaceSOAP.getTestDoc(); > // Prepare HTTP post > PostMethod post = new PostMethod(strURL); > post.setRequestEntity(new StringRequestEntity(requestDoc, > "text/xml; charset=UTF-8","UTF-8")); > post.setRequestHeader("Content-type", "text/xml; charset=UTF-8"); > post.setRequestHeader("SOAPAction", > "\"http://worksite.imanage.com/CreateWorkspace\ "" > ); > HttpClient httpclient = new HttpClient(); > post.getParams().setVersion(HttpVersion.HTTP_1_1); > httpclient.getParams().setParameter(CredentialsProvider.PROVIDER, > new PostXML.MyCredentialsProvider()); > try { > int result = httpclient.executeMethod(post); > System.out.println("Response status code: " + result); > System.out.println("Response body: "); > } finally { > post.releaseConnection(); > } > } > public static void getWithBasicAuth(String url, String username, > String password, String host, int port, String domain) throws > HttpException, IOException { > //http://www.developer.ibm.com/partnerworld/mem/index.html > GetMethod get = new GetMethod(url); > HttpClient httpclient = new HttpClient(); > httpclient.getState().setCredentials( > new AuthScope(host, port,domain), new > NTCredentials(username,password,host,domain)); > int result = httpclient.executeMethod(get); > // Display status code > System.out.println("Response status code: " + result); > // Display response > System.out.println("Response body: "); > System.out.println(get.getResponseBodyAsString()); > > > } > public static class MyCredentialsProvider implements > CredentialsProvider { > > /** > * @see > org.apache.commons.httpclient.auth.CredentialsProvider#getCredentials(AuthScheme, > String, int, boolean) > */ > public Credentials getCredentials( > AuthScheme scheme, > String host, > int port, > boolean proxy) > throws CredentialsNotAvailableException { > return new UsernamePasswordCredentials( > "myusername","mypassword"); > } > > } > } > > This message is being sent from a law firm and may contain confidential or privileged information. If you are not the intended recipient, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] This message is being sent from a law firm and may contain confidential or privileged information. If you are not the intended recipient, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.
