2011-07-09 38 views
0

我需要一些指針。所以基本上我所做的是我創建了一個用於登錄到Web服務器的表單。所以,從響應代碼我可以通過wireshark獲得200的狀態代碼。這個瀏覽器登錄系統的正常路徑是使用cookie登錄Android的機器人

password + id> index.php>(if true)> index2.php但是,在Android應用程序,顯然它不會重定向,但我不知道它的應該?我試過使用Jsoup.connect(URL).cookie(sessionid,我不知道爲什麼,總是空).get();但由於它始終爲空,因此不起作用。如果正確的cookie標題是「PHPSESSID = somenumiericavalue」或者我從wireshark看錯誤的東西?另一件事,正如我所說我嘗試使用Jsoup.connect方法,也HttpURLConnection也Android HTTP login questions,但是,當涉及到我需要檢查標題和價值我丟失的部分:(。

if(response.getStatusLine().getStatusCode() == 200){ 
         HttpEntity entity = response.getEntity(); 
         Log.d("login", "success!"); 
         if(entity != null){ 
         cookies = client.getCookieStore(); 

時龐貝氏說做一些更多的東西,我在這裏失去了。絕對失去了在這裏。另一部分是它使用Wireshark的下一個部分。失去了太多。:(

回答

0

的餅乾一般應HttpClient的處理正確:你只需要使用相同的HttpClient實例登錄,以便用於實際的請求,並且Cookie應該在所有請求中維護。例如,這是我使用的特定登錄順序:在我的情況下,我使用一個Locati重定向作爲成功符號,但您可以改爲使用200 OK狀態行。如果這返回true,那麼HttpClient實例session中的cookie存儲具有適當的cookie,我可以很好地訪問任何受保護的URL。

public synchronized boolean login() throws Exception 
    { 
    HttpGet httpget; 
    HttpPost httpost = new HttpPost(base + "auth/login"); 
    List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
    nvps.add(new BasicNameValuePair("username", this.username)); 
    nvps.add(new BasicNameValuePair("password", this.password)); 
    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 
    HttpResponse r = session.execute(httpost); 

    // verify it succeeded 
    HttpEntity entity = r.getEntity(); 
    Header lhdr = r.getFirstHeader("Location"); 
    if (lhdr != null) { 
     // failed 
     System.err.println("Login request failed:"); 
     if (entity != null && entity.getContentEncoding() != null) { 
     dump("Login Request", 
      new InputStreamReader(entity.getContent(), entity 
         .getContentEncoding().getValue())); 
     } 
     return false; 
    } 
    if (entity != null) entity.consumeContent();  
    return true; 
    }