2010-09-15 2 views
3

J'ai une connexion correcte en utilisant HttpRequest pour fonctionner. Il imprime la forme html correcte de la page de connexion dans mon toast (juste pour tester). Maintenant, je veux définir un cookie à partir de cette demande. Comment est-ce possible? Si nécessaire, je peux fournir du code.Définir le cookie à partir de la demande HTTP

Je connais déjà la classe CookieManager, mais comment puis-je la réussir?

Merci d'avance!

Mon 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(); 
}   

Eh bien, cela est à peu près tout. La fonction convertStreamToString() convertit l'InputStream en une chaîne (code HTML), que je "grille" pour le tester (donc ça marche), donc le code fonctionne bien. Maintenant, pour mettre le cookie. :-)

C'est ce que je suis arrivé pour l'instant:

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

Quand je suis connecté, l'identifiant CookieList 1 contient une valeur, sinon la valeur est standard. Donc, pour l'instant, je connais la différence de valeur, mais comment puis-je continuer?

Répondre

2

Je pense que Android est livré avec Apache HttpClient 4.0.

Vous pouvez consulter le sujet Chapter 3. HTTP state management du tutoriel HttpClient.

Vous pouvez également consulter des questions similaires sur le SO:

Android project using httpclient --> http.client (apache), post/get method

How do I manage cookies with HttpClient in Android and/or Java?

Vérifiez également cet exemple pour l'utilisation: http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java

+0

Eh bien, j'ai examiné maintenant, mais je aucune idée comment mettre en œuvre (comme utiliser la réponse pour définir un cookie). – Curtain

+0

Vu votre montage, va regarder ça. – Curtain

+0

Cochez cet exemple pour l'utiliser: http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java – YoK

Questions connexes