tags:

views:

82

answers:

1

Hello! I have get a correct login using HttpRequest to work. It prints the correct html form of the logn page in my toast (just for testing). Now I want to set a cookie from that request. How is this possible? If it necessary I can provide some code.

I already know about the CookieManager class, but how can I successfully do it?

Thanks in advance!

My code:

    public String getPostRequest(String url, String user, String pass) {
    HttpClient postClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    HttpResponse response;

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("login", user));
        nameValuePairs.add(new BasicNameValuePair("pass", pass));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));      

        response = postClient.execute(httpPost);

        if(response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();  
                String result = convertStreamToString(instream);                   
                instream.close();             

                return result;      
            }
        }
    } catch (Exception e) {}
    Toast.makeText(getApplicationContext(),
            "Connection failed",
            Toast.LENGTH_SHORT).show();
    return null; 
}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}           

Well, this is pretty much it. convertStreamToString() function converts the InputStream into a String (plain HTML code), which I "toast" out to just test it (so it work), so the code is working though. Now to set the cookie. :-)

This is what I've reached for now:

// inside my if (entity != null) statement
List<Cookie> cookies = postClient.getCookieStore().getCookies();
String result = cookies.get(1).toString();
                    return result;

When I have logged in, the CookieList id 1 contains a value, otherwise the value is standard. So for now I know the difference in value, but how can I continue?

+2  A: 

I think Android ships with Apache HttpClient 4.0.

You can check Chapter 3. HTTP state management topic from HttpClient Tutorial.

You can also refer similar questions on SO:

http://stackoverflow.com/questions/874227/android-project-using-httpclient-http-client-apache-post-get-method

http://stackoverflow.com/questions/3587254/how-do-i-manage-cookies-with-httpclient-in-android-and-or-java/3587332#3587332

Also Check this example for usage: http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java

YoK
Well, I have looked into this now, but I have no idea how to implement (as use the Response to set a cookie).
Julian Assange
Saw your edit, will look at that.
Julian Assange
Check this example for usage: http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java
YoK
Share your code if information from tutorial and example doesn't workout
YoK
Okey, see edit soon, glad you help me, really appreciate it.
Julian Assange
Now can you see my edited code.
Julian Assange
Did you try anything for cookies which failed ? Please include it. I think tutorial and examples contain enough info to get you through :).
YoK
Well, I wanna set the cookie in if (entity != null) statement. But if the login has failed, do not set the cookie otherwise set. The problem is as said how to implement it. I'm really new to this.
Julian Assange
I have provided you with pointers. Read it and try. On Stackoverflow everyone generally helps only if something is tried and didn't work :).
YoK
@Yok: Now I have advanced a little bit. You were absolutely right about the Read and Try thing. Though now I have to do some comparison, but I'm not sure how? :/
Julian Assange
Well I solved it! Thank you very much!
Julian Assange