You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by pcable <PC...@drhorton.com> on 2009/04/30 22:32:30 UTC

Issue with HttpMethodRetryHandler

I have the code working to actually bring back the data that I need. 
However, in the process of actually getting it working, I noticed that if
the authentication had any issues (like acct locked out, or unauthorized)
the program would go into an infinite loop of trying to authenticate.  So, I
am trying to fix that before I take this code to production.

The code is as follows:

main(){
	        try { 
	            HttpClient client = new HttpClient(); 
			    List authPrefs = new ArrayList(1);
			    authPrefs.add(AuthPolicy.DIGEST);
			    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,
authPrefs);	
			    client.getParams().setParameter(
				       CredentialsProvider.PROVIDER, new ConsoleAuthPrompter());
		   
		        PostMethod gm = new
PostMethod("http://~~servername~~/Maps/MapsStaging/PlatPDFHandler.ashx?pid=3&isbw=0"); 
			    gm.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
myretryhandler);
		        int status = client.executeMethod(gm); 
		        System.out.println("Response Status: "+ status); 
		        //String result = gm.getResponseBodyAsString(); 
		        //System.out.println(result); 
		        InputStream iStream = gm.getResponseBodyAsStream();
				DataOutputStream oStream = new DataOutputStream(System.out);
		        //FileInputStream iStream = new
FileInputStream(fullFilenameAndPath.toString());
		        byte[] buffer = new byte[1024];
		        int bytesRead = iStream.read(buffer);
		        while(bytesRead > -1)
		        {
		             oStream.write(buffer, 0, bytesRead);      
		             bytesRead = iStream.read(buffer);
		             //System.out.println("bytesRead="+bytesRead);
		        }
		        oStream.flush();
		        oStream.close();
		        //System.out.println("after stream = "+iStream.read());
		        iStream.close();
		        gm.releaseConnection();
	        } catch (IOException e) { 
	        	e.printStackTrace(); 
	        } 

}

		HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
		    public boolean retryMethod(
		        final HttpMethod method, 
		        final IOException exception, 
		        int executionCount) {
		        if (executionCount >= 5) {
		            // Do not retry if over max retry count
		            return false;
		        }
		        if (exception instanceof NoHttpResponseException) {
		            // Retry if the server dropped connection on us
		            return true;
		        }
		        if (!method.isRequestSent()) {
		            // Retry if the request has not been sent fully or
		            // if it's OK to retry methods that have been sent
		            return true;
		        }
		        // otherwise do not retry
		        return false;
		    }
		};



I have added the HttpMethodRetryHandler as indicated on the HttpClient main
page, however it doesn't seem to be helping in any way.

Any help with this last remaining issue, would be GREATLY appreciated.
Thanks,
PCable
-- 
View this message in context: http://www.nabble.com/Issue-with-HttpMethodRetryHandler-tp23322612p23322612.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Issue with HttpMethodRetryHandler

Posted by Oleg Kalnichevski <ol...@apache.org>.
pcable wrote:
> I have the code working to actually bring back the data that I need. 
> However, in the process of actually getting it working, I noticed that if
> the authentication had any issues (like acct locked out, or unauthorized)
> the program would go into an infinite loop of trying to authenticate.  So, I
> am trying to fix that before I take this code to production.
> 

Most likely this is due to the the credentials provider implementation. 
   Credentials provider must be stateful: that is, they must keep track 
of previous authentication attempts and ought not return the same 
credentials for a previously failed authentication attempt.

http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/auth/CredentialsProvider.html

Basically CredentialsProvider in its present form is broken and should 
be avoided. Just populate the HttpState with known credentials before 
executing requests.

Alternatively, consider upgrading to HttpClient 4.0 which has a better 
authentication framework.

Hope this helps

Oleg



> The code is as follows:
> 
> main(){
> 	        try { 
> 	            HttpClient client = new HttpClient(); 
> 			    List authPrefs = new ArrayList(1);
> 			    authPrefs.add(AuthPolicy.DIGEST);
> 			    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY,
> authPrefs);	
> 			    client.getParams().setParameter(
> 				       CredentialsProvider.PROVIDER, new ConsoleAuthPrompter());
> 		   
> 		        PostMethod gm = new
> PostMethod("http://~~servername~~/Maps/MapsStaging/PlatPDFHandler.ashx?pid=3&isbw=0"); 
> 			    gm.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
> myretryhandler);
> 		        int status = client.executeMethod(gm); 
> 		        System.out.println("Response Status: "+ status); 
> 		        //String result = gm.getResponseBodyAsString(); 
> 		        //System.out.println(result); 
> 		        InputStream iStream = gm.getResponseBodyAsStream();
> 				DataOutputStream oStream = new DataOutputStream(System.out);
> 		        //FileInputStream iStream = new
> FileInputStream(fullFilenameAndPath.toString());
> 		        byte[] buffer = new byte[1024];
> 		        int bytesRead = iStream.read(buffer);
> 		        while(bytesRead > -1)
> 		        {
> 		             oStream.write(buffer, 0, bytesRead);      
> 		             bytesRead = iStream.read(buffer);
> 		             //System.out.println("bytesRead="+bytesRead);
> 		        }
> 		        oStream.flush();
> 		        oStream.close();
> 		        //System.out.println("after stream = "+iStream.read());
> 		        iStream.close();
> 		        gm.releaseConnection();
> 	        } catch (IOException e) { 
> 	        	e.printStackTrace(); 
> 	        } 
> 
> }
> 
> 		HttpMethodRetryHandler myretryhandler = new HttpMethodRetryHandler() {
> 		    public boolean retryMethod(
> 		        final HttpMethod method, 
> 		        final IOException exception, 
> 		        int executionCount) {
> 		        if (executionCount >= 5) {
> 		            // Do not retry if over max retry count
> 		            return false;
> 		        }
> 		        if (exception instanceof NoHttpResponseException) {
> 		            // Retry if the server dropped connection on us
> 		            return true;
> 		        }
> 		        if (!method.isRequestSent()) {
> 		            // Retry if the request has not been sent fully or
> 		            // if it's OK to retry methods that have been sent
> 		            return true;
> 		        }
> 		        // otherwise do not retry
> 		        return false;
> 		    }
> 		};
> 
> 
> 
> I have added the HttpMethodRetryHandler as indicated on the HttpClient main
> page, however it doesn't seem to be helping in any way.
> 
> Any help with this last remaining issue, would be GREATLY appreciated.
> Thanks,
> PCable


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org