Android HttpClient会话后问题?如何知道会话是否已创建?

Android HttpClient会话后问题?如何知道会话是否已创建?,android,post,httpclient,Android,Post,Httpclient,这是的后续问题,我已成功发布到服务器并收到200个代码,但当我尝试移动到另一个页面时,它无法识别我已登录。我想知道这是否是一个会话问题,或者我是否需要在发布后遵循重定向。我将如何执行重定向?再次感谢您的帮助。下面是一个简单的HttpClient/POST应用程序,我通过示例创建它,以帮助我快速测试任何更改 public class HttpClientTest extends Activity{ HttpClient client = new DefaultHttpClient(); @Ov

这是的后续问题,我已成功发布到服务器并收到200个代码,但当我尝试移动到另一个页面时,它无法识别我已登录。我想知道这是否是一个会话问题,或者我是否需要在发布后遵循重定向。我将如何执行重定向?再次感谢您的帮助。下面是一个简单的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按钮,该按钮将我发送到一个页面,您必须登录才能访问该页面时,我会看到未登录页面。有什么想法吗?

经过大量的研究和示例之后,我终于登录到了我尝试的网站。显然,这是一个问题,我没有消费的实体从回应,即饼干。我创建了一个简单的活动,其主要目的是获取并发布到站点,然后将结果输出到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.dTAG,检查登录:+Parser.requestresponse;通过一个解析器类打印出该站点的html,我用它来验证它实际上是一个需要成功登录的页面。经过大量研究和许多示例之后,我终于成功登录到了我尝试的站点。显然,这是一个问题,我没有消费的实体从回应,即饼干。我创建了一个简单的活动,其主要目的是获取并发布到站点,然后将结果输出到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.dTAG,检查登录:+Parser.requestresponse;通过一个解析器类打印出该站点的html,我用它来验证它实际上是一个需要成功登录的页面

我知道,有点晚了,但我也遇到了这篇文章,我想在这里找到这个Ansare的链接:它说你应该重用处理会话管理的HttpClient,也就是说,将HttpClient设置为静态并只实例化一次。 但我不知道这是否是你要求的全部事实。对我来说是这样


干杯

我知道,有点晚了,但我也看到了这篇文章,我想在这里找到这个Ansare的链接:上面说你应该重用处理会话管理的HttpClient,也就是说,将HttpClient设置为静态并只实例化一次。 但我不知道这是否是你要求的全部事实。对我来说是这样


干杯

你需要问谁编写了你正在访问的Web应用程序。只有他们能告诉你他们想让你怎么做你想做的事。我害怕这样。感谢您的回复。可能是一些cookie问题。。请检查一下,我正在研究饼干,我开始认为这可能是一个参考问题。我将尝试编辑标题,以完全模仿Firefox。你需要问这个问题谁编写了你正在访问的Web应用程序。只有他们能告诉你他们想让你怎么做你想做的事。我害怕这样。感谢您的回复。可能是一些cookie问题。。请检查一下,我正在研究饼干,我开始认为这可能是一个参考问题。我将尝试编辑标题以完全模仿Firefox。