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

发布于 2024-09-16 05:12:03 字数 2617 浏览 3 评论 0原文

这是 通过 httpclient android 发送 html 命令 的后续问题,我有成功发布到服务器并收到 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 按钮时,该按钮将我发送到一个您必须登录才能访问的页面,我得到了未登录在页面中。有什么想法吗?

This is a follow up question of Sending html commands over httpclient android , I have successfully POSTed to the server and recieved 200 code but when I attempt to move to another page it does not recognize that I have logged in. I am wondering if it is a session issue or if i need to follow the redirect after the POST. How would I go about following a redirect? Again any help is greatly appreciated. Here is a simple HttpClient / POST app I created from examples in order to help me quickly test any changes.

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

}

I first press the login button on the UI which gives me the Http/1.1 200 OK response code, but when I press the btnFetch button which sends me to a page in which you must but logged in to access, I get the not logged in page. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我闹别瞎闹 2024-09-23 05:12:03

我知道,有点晚了,但我也看到了这篇文章,我想在这里找到这个答案的链接: Android 会话管理 表示您应该重用处理会话管理的 HttpClient ,即。将 HttpClient 设为静态并仅实例化一次。
但我不知道这是否符合您的要求。对我来说是的。

干杯!

I know, quite a little bit late, but I also come across this posting and I want the link to this answere here: Android session management which says that you should reuse your HttpClient which handles Session Management, ie. make HttpClient static and instantiate it only once.
But I dont know if this is the whole truth for your requirements. For me it was.

Cheers!

匿名的好友 2024-09-23 05:12:03

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

After much research and many examples later I have finally managed to login to the site I was attempting. Apparently It was an issue with me not consuming the entity from the response, ie the cookies. I created a simple activity who's main purpose is to GET and POST to the site then spit out the result into LogCat. Perhaps this may help others.

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();
    }

}

}

the last log, Log.d(TAG, "Check for login: " + Parser.request(response)); prints out the html of the site by going through a parser class, which I used to verify that it was infact a page that required successful login

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文