views:

33

answers:

1

This is a follow up question of http://stackoverflow.com/questions/3550593/sending-html-commands-over-httpclient-android , I have successfully POSTed to the server and recieved 200 code but when I attempt to move to another page it does not recognize that I have logged in. I am wondering if it is a session issue or if i need to follow the redirect after the POST. How would I go about following a redirect? Again any help is greatly appreciated. Here is a simple HttpClient / POST app I created from examples in order to help me quickly test any changes.

public class HttpClientTest extends Activity{

HttpClient client = new DefaultHttpClient();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button btnFetch = (Button)findViewById(R.id.button);
    final TextView txtResult = (TextView)findViewById(R.id.content);
    final Button login = (Button)findViewById(R.id.button2);


    btnFetch.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            getRequest(txtResult);
        }
    });

    login.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            try {
                login(txtResult);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}

public void getRequest(TextView txtResult){
    HttpGet request = new HttpGet("http://gc.gamestotal.com/i.cfm?f=com_empire&cm=3");
    try{
        HttpResponse response = client.execute(request);
        txtResult.setText(Parser.request(response));
    }catch(Exception ex){
        txtResult.setText("Failed!");
    }
}

public void login(TextView txtResult) throws UnsupportedEncodingException, IOException{
    String action = "i.cfm?&1028&p=login&se=4";
    String yourServer = "http://gc.gamestotal.com/";
    HttpPost post = new HttpPost(yourServer + action);
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("nic", "user"));
    params.add(new BasicNameValuePair("password", "password"));
    params.add(new BasicNameValuePair("server", "4"));
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);

    try{
        HttpResponse response = client.execute(post);
        txtResult.setText(response.getEntity().toString());
    }catch(Exception ex){
        txtResult.setText("Failed!");
    }
}

}

I first press the login button on the UI which gives me the Http/1.1 200 OK response code, but when I press the btnFetch button which sends me to a page in which you must but logged in to access, I get the not logged in page. Any ideas?

A: 

After much research and many examples later I have finally managed to login to the site I was attempting. Apparently It was an issue with me not consuming the entity from the response, ie the cookies. I created a simple activity who's main purpose is to GET and POST to the site then spit out the result into LogCat. Perhaps this may help others.

public class HttpClientTest extends Activity{

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request;
HttpEntity entity;
List<Cookie> cookies;
HttpResponse response;
HttpPost post;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        getRequest();
    } catch (Exception e) {
        Log.d("My Activity", "Failed");
        e.printStackTrace();
    }
}

public void getRequest() throws Exception
{
    final String TAG = "MyActivity";
    request = new HttpGet("http://some.site.com/");
    response = client.execute(request);
    entity = response.getEntity();
    Log.d(TAG, "Login form get: " + response.getStatusLine());
    if(entity != null)
    {
        entity.consumeContent();
    }
    Log.d(TAG, "Initial set of cookies:");

    cookies = client.getCookieStore().getCookies();
    if (cookies.isEmpty())
    {
        Log.d(TAG, "None");
    }
    else
    {
        for(int i = 0; i<cookies.size(); i++)
        {
            Log.d(TAG, "- " + cookies.get(i));
        }
    }
    String action = "i.cfm?&1028&p=login&se=4";
    String yourServer = "http://some.site.com/";
    post = new HttpPost(yourServer + action);

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("nic", "username"));
    params.add(new BasicNameValuePair("password", "password"));
    params.add(new BasicNameValuePair("server", "4"));

    post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));


    response = client.execute(post);
    entity = response.getEntity();

    Log.d(TAG, "Login form get: " + response.getStatusLine());
    if(entity != null){
        entity.consumeContent();
    }

    Log.d(TAG, "Post logon cookies:");
    cookies = client.getCookieStore().getCookies();
    if (cookies.isEmpty())
    {
        Log.d(TAG, "None");
    } 
    else
    {
        for (int i = 0; i < cookies.size(); i++) 
        {
            Log.d(TAG, "- " + cookies.get(i));
        }
    }


    request = new HttpGet("http://some.site.com/i.cfm?f=com_empire&amp;cm=3");

    response = client.execute(request);
    Log.d(TAG, "Check for login: " + Parser.request(response));
    if(entity != null)
    {
        entity.consumeContent();
    }

}

}

the last log, Log.d(TAG, "Check for login: " + Parser.request(response)); prints out the html of the site by going through a parser class, which I used to verify that it was infact a page that required successful login

Alejandro Huerta