This is my first post here, but its one I've been struggling with for two days without any result.
I am installing a Java application on a hosted machine that has an internet proxy server.
When I use web browsers on this particular machine, they don't require any authentication - just the proxy host and port details.
When I run the following simple program that uses a UrlConnection, all it requires to get it running correctly are the proxy host and port also.
public class TestProxyConnectionUrlConnection {
public void runUrlConnection() {
try {
String host = "proxy-local.net";
String port = "8080";
System.setProperty( "http.proxyHost", host);
System.setProperty( "http.proxyPort", port);
System.setProperty( "http.useProxy", "true");
URL url = new URL("http://mydomainb.com/myservice");
URLConnection yc = url.openConnection();
yc.setRequestProperty("Connection", "keep-alive");
yc.setDoOutput(true);
yc.setDoInput(true);
DataOutputStream wr = new DataOutputStream(yc.getOutputStream());
wr.writeBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xmlcontent>content</xmlcontent>");
wr.flush();
wr.close();
BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream()));
try {
String inputLine;
String response = "";
while ((inputLine = in.readLine()) != null) {
response = inputLine;
}
}
finally {
in.close();
}
System.out.println(response);
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
However, when I run code that uses Apache Commons HttpClient to talk to the server, as per the next bit of code, the proxy server is responding with a 407 error, and requesting NTLM or Kerberos authentication.
* Note we are stuck in using HttpCommons v3.1 for the time being due to a web-service product that uses it also.
public class TestProxyConnectionHttpCommons {
public void runHttpClient() throws Exception {
HttpClient httpclient = new HttpClient();
try {
String host = "proxy-local.net";
String port = "8080";
ProxyHost pxy = new ProxyHost(host, Integer.parseInt(port));
httpclient.getHostConfiguration().setProxyHost( pxy);
PostMethod currentMethod = new PostMethod( "http://mydomainb.com/myservice");
currentMethod.setRequestHeader( "Content-type", "text/xml; charset=UTF-8");
currentMethod.setRequestBody( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xmlcontent>content</xmlcontent>");
// The following line doesn't have any impact.
//currentMethod.setDoAuthentication(false);
final int iResult = httpclient.executeMethod( currentMethod);
String sResult = currentMethod.getResponseBodyAsString();
System.out.println(sResult);
}
finally {
httpclient = null;
}
}
}
Does anyone know why HttpClient would be resulting in an authentication challenge, but using a standard UrlConnection gets through the proxy server fine? Is there something fundamental that I'm doing wrong?