php登录android上的cookie

时间:2011-07-09 14:14:42

标签: php android cookies jsoup httpconnection

我需要一些指示。基本上我所做的就是创建了一个用于登录Web服务器的表单。因此,从响应代码我能够通过wireshark获得200的状态代码。此浏览器登录系统的正常路径是

密码+ ID> index.php>(如果为真)> index2.php但是,在Android应用程序上,显然它不会重定向,我不确定它的假设是什么?我已经尝试过使用Jsoup.connect(URL).cookie(我不知道为什么会起来的sessionid,总是为null).get();但由于它始终为空,因此无法正常工作。如果正确的是cookie标题是“PHPSESSID = somenumiericavalue”,还是我从wireshark看错了什么?另一件事,正如我所说,我尝试使用Jsoup.connect方法和HttpURLConnection也跟随Android HTTP login questions,但是,当涉及到我需要检查标题和值的部分我丢失了:( 。

if(response.getStatusLine().getStatusCode() == 200){
                        HttpEntity entity = response.getEntity();
                        Log.d("login", "success!");
                        if(entity != null){
                         cookies = client.getCookieStore();
当Pompe说要做更多的事情时,我迷失在这里。绝对迷失在这里。另一部分是下一部分,它使用wireshark。也输了。 :(

1 个答案:

答案 0 :(得分:0)

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