Set cookie from HTTP Request
<p>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. </p>

<p>I already know about the CookieManager class, but how can I successfully do it?</p>

<p>Thanks in advance!</p>

<p>My code:</p>

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

    try {
        List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(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();
}           
</code></pre>

<p>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. :-)</p>

<p>This is what I've reached for now:</p>

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

<p>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?</p>
