1

因此,我尝试使用 Apache 的 HttpClient v.4 登录网站(在此示例中为 myspace),但我不确定在此过程中哪里出错了,当我测试此代码时,登录后 Cookie 与初始登录 cookie,但不应该这样。

我在网上四处查看是否有其他人尝试过,但我还没有找到适合我的好资源。

我正在使用这个例子:apa​​che HttpClient to access facebook ( Facebook)

 try{
        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet("http://www.myspace.com/auth/form");

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }
        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        HttpPost httpost = new HttpPost("http://www.myspace.com/auth/form");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("email", "someEmail"));
        nvps.add(new BasicNameValuePair("password", "somePassword"));

        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        response = httpclient.execute(httpost);
        entity = response.getEntity();
        //System.out.println("Double check we've got right page " + EntityUtils.toString(entity));

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
        httpclient.getConnectionManager().shutdown();
    }
    catch(Exception e){e.printStackTrace();}

示例饼干:

    [version: 0][name: MSCOUNTRY][value: US][domain: .myspace.com][path: /][expiry: Tue Jun 12 23:22:15 EDT 2012]
[version: 0][name: MSCulture][value: IP=XXX.XXX.XXXX&IPCulture=en-US&PreferredCulture=en-US&PreferredCulturePending=&Country=VVM=&ForcedExpiration=0&timeZone=0&myStuffDma=&myStuffMarket=&USRLOC=RandomUserLocString==&UserFirstVisit=1][domain: .myspace.com][path: /][expiry: Tue Jun 12 23:22:15 EDT 2012]
[version: 0][name: SessionDDF2][value: RandomStringHere==][domain: .myspace.com][path: /][expiry: Sat Jun 05 23:22:15 EDT 2032]
4

2 回答 2

1

为什么您认为初始 cookie 和后期 cookie 应该不同?应用程序服务器端分配一组 cookie 给客户端,并且通常在整个会话期间保持不变。

应用程序服务器关联一组与会话相关的属性(使用 cookie 作为查找),这就是决定会话是否“登录”的原因。

于 2012-06-06T01:25:17.187 回答
0

这里没有提到的一件事是,为了成功打开经过身份验证的URLConnection文件,您需要在以下位置设置身份验证后收到的 cookie URLConnection

    URL myUrl = new URL("http://yourdomain/your/page);

    URLConnection urlConn = myUrl.openConnection();

    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("- " + cookies.get(i).toString());
        urlConn.setRequestProperty("Cookie",cookies.get(i).getName() + "=" + cookies.get(i).getValue());
    }
    urlConn.connect();

    InputStream io = (InputStream) urlConn.getContent();


    // print out the response
    List<String> readLines = IOUtils.readLines(io);

    for (String line : readLines) {
        System.out.println (line);
    }
于 2013-03-12T23:45:41.460 回答