【问题标题】:HttpClient Post session issue? How do I know if session has been created?HttpClient Post 会话问题?我如何知道会话是否已创建?
【发布时间】:2010-08-25 01:21:50
【问题描述】:

这是Sending html commands over httpclient android 的后续问题,我已成功发布到服务器并收到 200 代码,但是当我尝试移动到另一个页面时,它无法识别我已登录。我想知道是不是会话问题,或者如果我需要在 POST 之后遵循重定向。我将如何进行重定向?再次非常感谢任何帮助。这是我从示例中创建的一个简单的 HttpClient / POST 应用程序,以帮助我快速测试任何更改。

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!");
    }
}

}

我首先按下 UI 上的登录按钮,它给了我 Http/1.1 200 OK 响应代码,但是当我按下 btnFetch 按钮将我发送到您必须登录才能访问的页面时,我得到了未登录页面。有什么想法吗?

【问题讨论】:

  • 您需要向编写您正在访问的 Web 应用程序的人提出这个问题。只有他们才能告诉您他们希望您如何做您想做的事情。
  • 我害怕这样。感谢您的回复。
  • 可能是一些cookie问题..请检查
  • 我正在研究找出 cookie,我开始认为这可能是一个“推荐人”问题。我将尝试编辑标题以完全模仿 Firefox。

标签: android post httpclient


【解决方案1】:

我知道,有点晚了,但我也看到了这个帖子,我想要这个答案的链接:Android session management,它说你应该重用处理会话管理的HttpClient,即。将HttpClient 设为静态并仅实例化一次。 但我不知道这是否是您要求的全部真相。对我来说是。

干杯!

【讨论】:

    【解决方案2】:

    经过大量研究和许多示例之后,我终于设法登录到我正在尝试的站点。显然,我没有从响应中消耗实体是一个问题,即 cookie。我创建了一个简单的活动,其主要目的是 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,我用它来验证它实际上是一个需要成功登录的页面

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2021-10-07
      • 2013-02-04
      • 2015-12-20
      相关资源
      最近更新 更多