2010-08-25 54 views
1

這是Sending html commands over httpclient android的後續問題,我已成功發佈到服務器並收到200個代碼,但是當我嘗試移動到另一個頁面時,它無法識別我已登錄。我想知道它是否是會話問題或者如果我需要在POST後關注重定向。我將如何去關於重定向?再次,任何幫助,不勝感激。這裏是一個簡單的HttpClient/POST應用程序,我通過示例創建,以幫助我快速測試任何更改。HttpClient發佈會話問題?我如何知道會話是否已創建?

public class HttpClientTest extends Activity{ 

HttpClient client = new DefaultHttpClient(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final Button btnFetch = (Button)findViewById(R.id.button); 
    final TextView txtResult = (TextView)findViewById(R.id.content); 
    final Button login = (Button)findViewById(R.id.button2); 


    btnFetch.setOnClickListener(new Button.OnClickListener(){ 
     public void onClick(View v){ 
      getRequest(txtResult); 
     } 
    }); 

    login.setOnClickListener(new Button.OnClickListener(){ 
     public void onClick(View v){ 
      try { 
       login(txtResult); 
      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

public void getRequest(TextView txtResult){ 
    HttpGet request = new HttpGet("http://gc.gamestotal.com/i.cfm?f=com_empire&cm=3"); 
    try{ 
     HttpResponse response = client.execute(request); 
     txtResult.setText(Parser.request(response)); 
    }catch(Exception ex){ 
     txtResult.setText("Failed!"); 
    } 
} 

public void login(TextView txtResult) throws UnsupportedEncodingException, IOException{ 
    String action = "i.cfm?&1028&p=login&se=4"; 
    String yourServer = "http://gc.gamestotal.com/"; 
    HttpPost post = new HttpPost(yourServer + action); 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("nic", "user")); 
    params.add(new BasicNameValuePair("password", "password")); 
    params.add(new BasicNameValuePair("server", "4")); 
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8"); 
    post.setEntity(entity); 

    try{ 
     HttpResponse response = client.execute(post); 
     txtResult.setText(response.getEntity().toString()); 
    }catch(Exception ex){ 
     txtResult.setText("Failed!"); 
    } 
} 

}

我第一次按上給我的HTTP/1.1 200 OK響應代碼UI中的登錄按鈕,但是當我按下btnFetch按鈕,送我到一個頁面中,您必須但登錄訪問,我得到沒有登錄頁面。有任何想法嗎?

+0

你需要問這個問題誰寫了你正在訪問的Web應用程序。只有他們可以告訴你他們希望你做你想做的事情。 – CommonsWare 2010-08-25 04:41:47

+0

我很害怕這樣的。感謝您的答覆。 – 2010-08-25 04:54:23

+0

可能是一些cookie問題..請檢查 – Vinay 2010-08-25 14:00:56

回答

0

經過大量研究和後來的許多例子,我終於設法登錄到我嘗試的網站。顯然這是一個問題,我沒有消費響應的實體,即餅乾。我創建了一個簡單的活動,主要目的是GET和POST到站點,然後將結果吐出到LogCat中。也許這可能會幫助其他人。

public class HttpClientTest extends Activity{ 

DefaultHttpClient client = new DefaultHttpClient(); 
HttpGet request; 
HttpEntity entity; 
List<Cookie> cookies; 
HttpResponse response; 
HttpPost post; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    try { 
     getRequest(); 
    } catch (Exception e) { 
     Log.d("My Activity", "Failed"); 
     e.printStackTrace(); 
    } 
} 

public void getRequest() throws Exception 
{ 
    final String TAG = "MyActivity"; 
    request = new HttpGet("http://some.site.com/"); 
    response = client.execute(request); 
    entity = response.getEntity(); 
    Log.d(TAG, "Login form get: " + response.getStatusLine()); 
    if(entity != null) 
    { 
     entity.consumeContent(); 
    } 
    Log.d(TAG, "Initial set of cookies:"); 

    cookies = client.getCookieStore().getCookies(); 
    if (cookies.isEmpty()) 
    { 
     Log.d(TAG, "None"); 
    } 
    else 
    { 
     for(int i = 0; i<cookies.size(); i++) 
     { 
      Log.d(TAG, "- " + cookies.get(i)); 
     } 
    } 
    String action = "i.cfm?&1028&p=login&se=4"; 
    String yourServer = "http://some.site.com/"; 
    post = new HttpPost(yourServer + action); 

    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("nic", "username")); 
    params.add(new BasicNameValuePair("password", "password")); 
    params.add(new BasicNameValuePair("server", "4")); 

    post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 


    response = client.execute(post); 
    entity = response.getEntity(); 

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

    Log.d(TAG, "Post logon cookies:"); 
    cookies = client.getCookieStore().getCookies(); 
    if (cookies.isEmpty()) 
    { 
     Log.d(TAG, "None"); 
    } 
    else 
    { 
     for (int i = 0; i < cookies.size(); i++) 
     { 
      Log.d(TAG, "- " + cookies.get(i)); 
     } 
    } 


    request = new HttpGet("http://some.site.com/i.cfm?f=com_empire&cm=3"); 

    response = client.execute(request); 
    Log.d(TAG, "Check for login: " + Parser.request(response)); 
    if(entity != null) 
    { 
     entity.consumeContent(); 
    } 

} 

} 

最後一個日誌Log.d(TAG,「Check for login:」+ Parser.request(response));通過解析器類打印出網站的html,我用它來驗證它實際上是一個需要成功登錄的頁面。

4

我知道,有點晚了,但我也遇到過這個帖子,我想要鏈接到這裏回答:Android session management其中說,你應該重用你的HttpClient處理會話管理,即。使HttpClient靜態並僅實例化它一次。 但我不知道這是否是您的要求的全部事實。對我來說是這樣。

乾杯!

+0

謝謝:)這爲我工作。 – 2012-06-01 11:44:53