I have included code and the log file that works correctly when the
proxy domain, username, and password are correct. The problem is that
the users will set up their credentials once (stored in a properties
file) and then not be prompted when the application is run. So when the
HttpClient is instantiated and the post method is executed, it retried
endlessly with a 407 error, even when I added:
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new
DefaultHttpMethodRetryHandler(3, false));
How do I get it to retry only 3 times?
/*====== Begin SendXML.java =========*/
import java.net.*;
import java.io.*;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.apache.commons.httpclient.auth.InvalidCredentialsException;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class SendXml{
private Log log;
public String streamToServer(String xmlData, String urlName){
String rtn=null;
String rtnValue=null;
log = LogFactory.getLog(this.getClass());
// HTTP client for sending the XML stream
HttpClient client = new HttpClient();
try {
// Get the properties
String urlString = "resource/system.properties";
Properties properties = SystemVars.getProperties();
// Set proxy configuration, if necessary
if (properties.getProperty("proxy.set").equalsIgnoreCase("Y")){
String proxyHost= properties.getProperty("proxy.host");
String proxyPort= properties.getProperty("proxy.port");
int intProxyPort= Integer.parseInt(proxyPort);
client.getHostConfiguration().setProxy(proxyHost,
intProxyPort);
client.getParams().setParameter(CredentialsProvider.PROVIDER, new
PropertiesAuthProvider());
log.info("Attempting connection through proxy server " +
proxyHost+ ":"+proxyPort);
}
// Set retry handler
client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new
DefaultHttpMethodRetryHandler(3, false));
PostMethod post = new PostMethod(urlName);
post.setRequestEntity(new InputStreamRequestEntity( new
StringBufferInputStream(xmlData), xmlData.length()));
post.setRequestHeader("Content-type", "text/xml;
charset=ISO-8859-1");
// Execute the post and display the results
try {
int result = client.executeMethod(post);
// Display status code
//System.out.println("Response status code: " + result);
log.debug("Response status code: " + result);
//System.out.println(post.getStatusLine());
// Display response
//System.out.println("Response body: ");
//System.out.println(post.getResponseBodyAsString());
log.debug("Response body: ");
log.debug(post.getResponseBodyAsString());
rtnValue= post.getResponseBodyAsString();
} finally {
// Release current connection to the connection pool
once you are done
post.releaseConnection();
}
if (rtnValue != null){
rtn = rtnValue;
}
} catch (MalformedURLException me) {
//me.printStackTrace();
log.error("SendXML Error: Malformed URL: " + me.toString());
} catch (ConnectException ce){
//System.out.println("Connection Failure");
//ce.printStackTrace();
log.error("SendXML Error: Connection Failure: " +
ce.toString());
} catch( InvalidCredentialsException ice){
log.error("SendXML Error: Invalid Credentials: " +
ice.toString());
} catch (IOException ie) {
//ie.printStackTrace();
log.error("SendXML Error: IO: " + ie.toString());
} catch (Exception e) {
//e.printStackTrace();
log.error("SendXML Error: General Exception: " + e.toString());
}
return rtn;
}
}
/*====== End SendXML.java =========*/
/*====== Begin PropertiesAuthProvider.java =========*/
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.apache.commons.httpclient.auth.NTLMScheme;
import org.apache.commons.httpclient.auth.RFC2617Scheme;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class PropertiesAuthProvider implements CredentialsProvider {
private Log log;
public PropertiesAuthProvider() {
super();
log = LogFactory.getLog(this.getClass());
}
//private String readConsole() throws IOException {
// return "";
//}
public Credentials getCredentials(
final AuthScheme authscheme,
final String host,
int port,
boolean proxy)
throws CredentialsNotAvailableException
{
if (authscheme == null) {
return null;
}
/* p.getProperty("proxy.host");
p.getProperty("proxy.port");
p.getProperty("proxy.domain");
p.getProperty("proxy.username");
p.getProperty("proxy.password");
*/
try{
// Get credentials from properties file
Properties p= SystemVars.getProperties();
if(p==null){
throw new CredentialsNotAvailableException("Properties
file not configured for proxy authentication.");
}
String domain = p.getProperty("proxy.domain");
//System.out.print("Enter username: ");
//String user = readConsole();
String user = p.getProperty("proxy.username");
//System.out.print("Enter password: ");
//String password = readConsole();
String password = p.getProperty("proxy.password");
if (user == null || user.equals("") || password == null ||
password.equals("")){
throw new CredentialsNotAvailableException("Either
username or password were not provided.");
}
if (authscheme instanceof NTLMScheme) {
log.info(host + ":" + port + " requires Windows
authentication");
//System.out.print("Enter domain: ");
//String domain = readConsole();
return new NTCredentials(user, password, host, domain);
} else if (authscheme instanceof RFC2617Scheme) {
log.info(host + ":" + port + " requires authentication
with the realm '" + authscheme.getRealm() + "'");
//System.out.print("Enter username: ");
//String user = readConsole();
return new UsernamePasswordCredentials(user, password);
} else {
log.error(host + ":" + port + " requires unsupported
authentication scheme: " + authscheme.getSchemeName());
throw new CredentialsNotAvailableException("Unsupported
authentication scheme: " + authscheme.getSchemeName());
}
} catch (IOException e) {
throw new CredentialsNotAvailableException(e.getMessage(), e);
}
}
}
/*====== End PropertiesAuthProvider.java =========*/
/*====== Begin Log file =========*/
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - << "HTTP/1.1 407
Proxy Authentication Required ( The ISA Server requires authorization to
fulfill the request. Access to the Web Proxy service is denied. )[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - << "Via: 1.1
MYPROXY-ISA1[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - <<
"Proxy-Authenticate: Negotiate[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - <<
"Proxy-Authenticate: Kerberos[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - <<
"Proxy-Authenticate: NTLM[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - <<
"Proxy-Authenticate: Basic realm="MYPROXY-ISA1.MYSITE.COM"[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - << "Connection:
close[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - <<
"Proxy-Connection: close[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - << "Pragma:
no-cache[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - <<
"Cache-Control: no-cache[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - <<
"Content-Type: text/html[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.header DEBUG - <<
"Content-Length: 713[\r][\n]"
08 Sep 2005 08:45:31,937 rsl.base.PropertiesAuthProvider INFO -
172.30.0.45:80 requires Windows authentication
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - <<
"<HTML><HEAD><TITLE>Error Message</TITLE>[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<META
http-equiv=Content-Type content="text/html; charset=windows-1252">[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<BODY>[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - <<
"<TABLE><TR><TD id=L_dt_1><B>Network Access Message: The page cannot be
displayed<B></TR></TABLE>[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - <<
"<TABLE><TR><TD height=15></TD></TR></TABLE>[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - <<
"<TABLE>[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<TR><TD
id=L_dt_2>Technical Information (for Support personnel)[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<UL>[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<LI
id=L_dt_3>Error Code: 407 Proxy Authentication Required. The ISA Server
requires authorization to fulfill the request. Access to the Web Proxy
service is denied. (12209)[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<LI
id=L_dt_4>IP Address: 172.30.0.45[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<LI
id=L_dt_5>Date: 9/8/2005 12:42:14 PM[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<LI
id=L_dt_6>Server: MYPROXY-ISA1.MYSITE.COM[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - << "<LI
id=L_dt_7>Source: proxy[\r][\n]"
08 Sep 2005 08:45:31,937 httpclient.wire.content DEBUG - <<
"</UL></TD></TR></TABLE></BODY></HTML>[\r][\n]"
08 Sep 2005 08:45:32,015 httpclient.wire.header DEBUG - >> "CONNECT
secure.mysite.com:443 HTTP/1.1"
08 Sep 2005 08:45:32,015 httpclient.wire.header DEBUG - >> "User-Agent:
Jakarta Commons-HttpClient/3.0-rc3[\r][\n]"
08 Sep 2005 08:45:32,015 httpclient.wire.header DEBUG - >>
"Proxy-Authorization: NTLM
TlRMTVNTUAABAAAABlIAAAgACAArAAAACwALACAAAAAxNzIuMzAuMC40NURFTlQuQ09N[\r][\n]"
08 Sep 2005 08:45:32,015 httpclient.wire.header DEBUG - >> "Host:
secure.mysite.com[\r][\n]"
08 Sep 2005 08:45:32,015 httpclient.wire.header DEBUG - >>
"Proxy-Connection: Keep-Alive[\r][\n]"
08 Sep 2005 08:45:32,015 httpclient.wire.header DEBUG - >> "[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - << "HTTP/1.1 407
Proxy Authentication Required ( Access is denied. )[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - << "Via: 1.1
MYPROXY-ISA1[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - <<
"Proxy-Authenticate: NTLM
TlRMTVNTUAACAAAABAAEADgAAAAGAoECzKVGuvX9Px4AAAAAAAAAAHYAdgA8AAAABQLODgAAAA9ERU5UAgAIAEQARQBOAFQAAQASAEQARQBOAFQALQBJAFMAQQAxAAQAEABEAEUATgBUAC4AQwBPAE0AAwAkAGQAZQBuAHQALQBpAHMAYQAxAC4ARABFAE4AVAAuAEMATwBNAAUAEABEAEUATgBUAC4AQwBPAE0AAAAAAA==[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - << "Connection:
Keep-Alive[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - <<
"Proxy-Connection: Keep-Alive[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - << "Pragma:
no-cache[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - <<
"Cache-Control: no-cache[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - <<
"Content-Type: text/html[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - <<
"Content-Length: 0[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - >> "CONNECT
secure.mysite.com:443 HTTP/1.1"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - >> "User-Agent:
Jakarta Commons-HttpClient/3.0-rc3[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - >>
"Proxy-Authorization: NTLM
TlRMTVNTUAADAAAAGAAYAFsAAAAAAAAAcwAAAAgACABAAAAACAAIAEgAAAALAAsAUAAAAAAAAABzAAAABlIAAERFTlQuQ09NS0dBU1RPTjExNzIuMzAuMC40NSOjshYbDwpUii9MzSxnaMrDlEyV23EYVQ==[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - >> "Host:
secure.mysite.com[\r][\n]"
08 Sep 2005 08:45:32,078 httpclient.wire.header DEBUG - >>
"Proxy-Connection: Keep-Alive[\r][\n]"
08 Sep 2005 08:45:32,093 httpclient.wire.header DEBUG - >> "[\r][\n]"
/*====== End Log file =========*/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]