views:

146

answers:

1

Hi,

I am trying to develop a java http client with apache httpcomponents 4.0.1. This client calls the page "https://myHost/myPage". This page is protected on the server by a JNDIRealm with a login form authentication, so when I try to get https://myHost/myPage I get a login page. I tried to bypass it unsuccessfully with the following code :

//I set my proxy
HttpHost proxy = new HttpHost("myProxyHost", myProxyPort);

//I add supported schemes
SchemeRegistry supportedSchemes = new SchemeRegistry();
supportedSchemes.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 80));
supportedSchemes.register(new Scheme("https", SSLSocketFactory
                .getSocketFactory(), 443));

// prepare parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
                supportedSchemes);
DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                proxy);

//I add my authentication information
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("myHost/myPage", 443),
new UsernamePasswordCredentials("username", "password"));


HttpHost host = new HttpHost("myHost", 443, "https");
HttpGet req = new HttpGet("/myPage");

//show the page
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String rsp = httpClient.execute(host, req, responseHandler);
System.out.println(rsp);

When I run this code, I always get the login page, not myPage. How can I apply my credential parameters to avoid this login form?

Any help would be fantastic

A: 

HttpClient doesn't support form login. What you are trying to do is Basic Auth, which does't work with form login.

You can simply trace the form post for login page and send the POST request from HttpClient.

ZZ Coder
Thanks! I'll try this.