Java Question Extend HttpClientWrapper

warwound

Expert
Licensed User
Longtime User
I need to call the DefaultHttpClient getCookieStore method, so created a library that wraps the BasicCookieStore and BasicClientCookie classes and then added a simple class that has a single method to call that method:

B4X:
@ShortName("HttpExtras")
public class HttpExtras {
   /**
    * Get the HttpClient's CookieStore.
    */
   public static BasicCookieStore GetBasicCookieStore(DefaultHttpClient HttpClient1){
      return new BasicCookieStore((org.apache.http.impl.client.BasicCookieStore) HttpClient1.getCookieStore());
   }
}

I'd assumed that the B4A HttpClientWrapper was a sub class of AbsObjectWrapper and that it wrapped the DefaultHttpClient so my method would be passed an unwrapped DefaultHttpClient from B4A - but i was wrong!
HttpClientWrapper base class is Object and the DefaultHttpClient is a private member - so not accesible.

Looks like my only option is to duplicate the B4A HTTP library so that i can get access to the DefaultHttpClient and call it's getCookieStore method - is that correct or is there an alternative (ie can i still use the B4A HTTP library)?

My aim is to login into a secure website over HTTPS with username and password POST key/values and get the auth session cookie to use in future requests.
This login replicates the functionality of a standard HTML form - it's not a HTTP basic authentication process, currently i'm using a hidden WebView to login and get the auth cookie (that works fine) but ideally want to use the HTTP library instead of the WebView.

The HTTP library works fine with HTTP sessions but not with HTTPS sessions, with HTTPS sessions the HttpResponse does not contain a Set-Cookie header.

Martin.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
HttpClient should have been a hidden public variable. As it is a private method you will need to use reflection to access it:
B4X:
 public static BasicCookieStore GetBasicCookieStore(DefaultHttpClient HttpClient1){
Field f = HttpClient1.getClass().getDeclaredField("client");
f.setAccessible(true);
DefaultHttpClient dhc = (DefaultHttpClient)f.get(HttpClient1);
 

warwound

Expert
Licensed User
Longtime User
Thanks that works perfctly.

I never knew reflection could be used to change a member's access modifier from private to public!

Martin.
 
Top