php login on android with cookie


php login on android with cookie

我这里需要一些指针。所以基本上我所做的是,我已经创建了一个表单,用于登录到一个web服务器。所以,从响应代码,我能够通过wireshark获得状态码200。这个浏览器登录系统的正常路径是

password + id> index.php>(如果为真)> index2.php但是,在android应用程序上,显然它不重定向虽然,我不确定它是否应该?我试过使用Jsoup.connect(URL)。Cookie (sessionid,我不知道为什么,总是null).get();但由于它总是为空,所以行不通。cookie头如果正确是"PHPSESSID= someenumiericvalue",或者我从wireshark看错误的东西?还有一件事,正如我说过的,我试过用Jsoup。连接方法和HttpURLConnection也,以下Android HTTP登录问题,但是,当涉及到我需要检查头和值的部分时,我丢失了:(。

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

当庞培说要做更多的事情时,我在这里迷路了。这里完全迷路了。另一个部分是接下来使用wireshark的部分。也失去了。(

cookie通常应该由HttpClient正确处理:您只需要使用与实际请求相同的HttpClient实例来登录,并且应该在所有请求中维护cookie。例如,这是我使用的一个特定登录序列:在我的例子中,我使用Location重定向作为成功符号,但是您可以使用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;
    }