0

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

密码 + id > index.php >(if true) > index2.php 但是,在 android 应用程序上,显然它没有重定向,我不确定它是否假设?我试过使用 Jsoup.connect(URL).cookie(sessionid 我不知道为什么,总是 null).get(); 但由于它始终为空,因此无法正常工作。如果正确,cookie 标头是“PHPSESSID=somenumiericavalue”,还是我从wireshark 看错了?还有一件事,正如我所说,我尝试使用 Jsoup.connect 方法和 HttpURLConnection 也遵循Android HTTP 登录问题,但是,当涉及到我需要检查标头和值的部分时,我迷路了 :(。

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

当庞贝说做更多的事情时,我迷路了。绝对迷失在这里。另一部分是使用wireshark 的下一部分。也输了。:(

4

1 回答 1

0

cookie 通常应该由 HttpClient 正确处理:您只需使用与实际请求相同的 HttpClient 实例来登录,并且应该在所有请求中维护 cookie。例如,这是我使用的特定登录序列:在我的例子中,我使用位置重定向作为成功符号,但您可以使用 200 OK 状态行来代替。如果返回 true,则 HttpClient 实例中的 cookie 存储session具有适当的 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;
    }
于 2011-07-11T15:15:47.773 回答