Hi all,
I have been trying to write a widget that connects to the Internet, parses
data and displays it. I have all the background functionality working, but I
don't appear to be able to get the cookies that are needed to log into the
website that provides the data to... persevere between post calls.
I first tried writing a custom HTTP/cookie helper class, but that didn't
work (cookies weren't displayed by the getCookies method).
I did some searching online and I found a class on t'internet that looked
good, and it started to keep cookies, but cookies don't appear to persevere
between post calls.
I fiddled around a little with the code and seem to have gotten them to
persevere... but that appears to have broken the http request (the code
segment that does the httpClient.execute throws an exception on the second
call and afterwards).
I can't seem to figure out how to make it work, and any help would be
greatly appreciated. The code I am using is below.
Thanks for any help.
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import android.util.Log;
public class httpHelper{
private static DefaultHttpClient httpClient = new DefaultHttpClient();
private static HttpContext localContext = new BasicHttpContext();
private static CookieStore cookieStore = new BasicCookieStore();
private static List<Cookie> cookies;
private static HttpResponse response = null;
private static HttpPost httpPost = null;
public httpHelper(){
httpClient.setCookieStore( cookieStore );
}
public void clearCookies() {
httpClient.getCookieStore().clear();
}
public void abort() {
try {
if( httpClient != null )
httpPost.abort();
} catch ( Exception e ) {
}
}
public void printCookies( ) {
if ( cookies != null )
for ( int i=0; i<cookies.size(); i++ ) {
Cookie cookie = cookies.get( i );
Log.i( "cookies", "val: " + cookie );
}
}
public HttpResponse postPage( String url, List<NameValuePair> params )
throws UnsupportedEncodingException {
// Set up the cookie policy
httpClient.getParams().setParameter( ClientPNames.COOKIE_POLICY,
CookiePolicy.RFC_2109 );
// Set up the post parameters
httpPost = new HttpPost( url );
if ( params!=null && params.size() > 0 ) {
UrlEncodedFormEntity ent = new UrlEncodedFormEntity( params, HTTP.UTF_8
);
httpPost.setEntity( ent );
}
response = null;
// Print the cookies for sanity's sake...
Log.i( "httpHelper.postPage", "Printing " +
httpClient.getCookieStore().getCookies().size() + " cookies." );
printCookies();
// Attempt the connection
try {
Log.i( "httpHelper.postPage", "Settings cookies..." );
if ( cookies != null )
for ( int i=0; i<cookies.size(); i++ )
httpClient.getCookieStore().addCookie( cookies.get(i) );
Log.i( "httpHelper.postPage", "Executing request..." );
response = httpClient.execute( httpPost, localContext );
Log.i( "httpHelper.postPage", "Getting cookies..." );
cookies = httpClient.getCookieStore().getCookies();
} catch (Exception e) {
Log.w( "httpHelper.postPage", "Execute threw: " + e );
}
// Save this for the next iteration
cookieStore = httpClient.getCookieStore();
printCookies();
return response;
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en