I have a Java webapp running on Tomcat 6 that downloads RSS feeds from remote URLs.
I use Rome to process RSS feeds and different formats for me. The connecting part looks like this:
try{ feedSource = new URL(rssObject.getAsset()); }catch(MalformedURLException mue){ logger.error(...); throw mue; } try{ URLConnection connection = feedSource.openConnection(); feed = new SyndFeedInput().build(new XmlReader(connection)); }catch(Exception){handle...}
The code works fine except for this new client, where they use proxies.
To use the proxy, I set the properties http.proxyHost and proxyPort:
System.setProperty("http.proxyHost", proxyHost); System.setProperty("http.proxyPort", proxyPort); System.setProperty("https.proxyHost", proxyHost); System.setProperty("https.proxyPort", proxyPort);
The HTTP GET is configured on the proxy in order, but now I get an HTTP 502 error (bad gateway or something like that).
Analyzing HTTP exchange with Wireshark, I noticed that proxy requires authentication. It sends HTTP 507. Java somehow tries to authenticate, but uses the wrong username and passwords. It seems that the hostname is used as the username, as for the password I do not know.
So I tried to implement the Authenticator method to specify username + password:
Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { logger.info(MessageFormat.format("Generating PasswordAuthentitcation for proxy authentication, using username={0} and password={1}.", username, password)); return new PasswordAuthentication(username, password.toCharArray()); } });
Now my problem is that it is ignored. The getPasswordAuthentication method is never called. I do not see the log statement in the log file and am using Wireshark. I see that it still uses the hostname as the username.
Why? It seems that Java is somehow trying to authenticate itself without consulting Authenticator.
The proxy server seems to be an MS device that uses NTLM for authentication. Is there a built-in mechanism in java to handle this? The machine on which the application is running is Win Server 2008 R2.