2012-09-05 30 views
6

Hola chicos estoy tratando de enviar una cookie con HTTP POST en AndroidEnvío de galleta con HTTP Post androide

aquí está mi código

webUrl = "XXXXXX"; 
     webView = (WebView) findViewById(R.id.webview); 
     try 
     { 
      CookieStore cookieStore = new BasicCookieStore(); 
      Cookie cookie = new BasicClientCookie("session_id", "1234"); 
      cookieStore.addCookie(cookie); 

      DefaultHttpClient httpclient = new DefaultHttpClient(); 

      BasicHttpContext mHttpContext = new BasicHttpContext(); 

      mHttpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

      HttpGet httpget = new HttpGet("XXXXX"); 

      HttpResponse response = httpclient.execute(httpget); 
      HttpEntity entity = response.getEntity(); 

      Log.i("TAG", "Login form get: " + response.getStatusLine()); 
      if (entity != null) { 
       entity.consumeContent(); 
      } 
      Log.i("TAG", "Initial set of cookies:"); 
      List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
      if (cookies.isEmpty()) { 
       Log.i("TAG", "None"); 
      } else { 
       for (int i = 0; i < cookies.size(); i++) { 
        Log.i("TAG", "- " + cookies.get(i).toString()); 
       } 
      } 

      HttpPost httpost = new HttpPost("XXXXX"); 

      List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 
      nvps.add(new BasicNameValuePair("session_id", "1234")); 
      nvps.add(new BasicNameValuePair("selected_cid", "1234")); 
      nvps.add(new BasicNameValuePair("selected_kaisai_id", "1234")); 
      nvps.add(new BasicNameValuePair("iid", "1234")); 
      nvps.add(new BasicNameValuePair("tid", "1234")); 

      httpost.setEntity(new UrlEncodedFormEntity(nvps)); 

      response = httpclient.execute(httpost); 
      entity = response.getEntity(); 

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

      Log.i("TAG", "Post logon cookies:"); 
      cookies = httpclient.getCookieStore().getCookies(); 
      if (cookies.isEmpty()) { 
       System.out.println("None"); 
      } else { 
       for (int i = 0; i < cookies.size(); i++) { 
        Log.i("TAG", "- " + cookies.get(i).toString()); 
       } 
      } 
      httpclient.getConnectionManager().shutdown();   
     } 
     catch(Exception e) 
     { 
      Log.i("TAG", e.getMessage()); 
     } 
     WebSettings webSettings = webView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     String postData = "session_id=1234"; 
     webView.postUrl(webUrl, EncodingUtils.getBytes(postData, "BASE64")); 

me pueden enviar los datos de Postdatum pero no la cookie la cookie no se muestra en la vista web aquí está el lado PHP

public function cookietestAction() 
    { 
     echo "mixi for netrecorder tester<br/>\n"; 

     echo "<br/>Cookies...<br/>\n\n"; 
     $request = new Zend_Controller_Request_Http(); 
     $sessionid = $request->getCookie('session_id'); 
     echo "<br/>\nsessionid = ".$sessionid."<br/>\n"; 

     $selected_cid = $request->getCookie('selected_cid'); 
     echo "\nselected_cid = ".$selected_cid."<br/>\n"; 

     $selected_kaisai_id = $request->getCookie('selected_kaisai_id'); 
     echo "\nselected_kaisai_id = ".$selected_kaisai_id."<br/>\n"; 

     $iid = $request->getCookie('iid'); 
     echo "\niid = ".$iid."<br/>\n"; 

     $tid = $request->getCookie('tid'); 
     echo "\ntid = ".$tid."<br/>\n"; 

     if($this->getRequest()->isPost()) 
     { 
      echo "<br/>POST data only...<br/>\n\n"; 
      $post = $this->getRequest()->getPost('session_id'); 
      echo "<br/>\nsession_id=".$post."<br/>\n"; 
      $post = $this->getRequest()->getPost('selected_cid'); 
      echo "selected_cid=".$post."<br/>\n"; 
      $post = $this->getRequest()->getPost('selected_kaisai_id'); 
      echo "selected_kaisai_id=".$post."<br/>\n"; 
      $post = $this->getRequest()->getPost('tid'); 
      echo "tid=".$post."<br/>\n"; 
      $post = $this->getRequest()->getPost('iid'); 
      echo "iid=".$post."<br/>\n"; 
     } 
} 

puede alguien decirme lo que está mal? gracias

Respuesta

17

añadir cokie utilizar de esta manera: hay que añadir en la cabecera

 post.addHeader("Cookie", " PHPSESSID="+PHPSESSID+"; gc_userid="+gc_user+"; gc_session="+gc); 

para que así debería ser:

 httpost.addHeader("Cookie","session_id = 1234;"...) 
+0

OK voy a tratar gracias droidhot! –

+0

lo siento droidhot, pero no funcionó, ¿tengo que agregar más código? gracias –

+0

lo siento, compruebe el código anterior –