Java apache Httpclient基于表单的登录myspace

时间:2012-06-06 00:19:00

标签: java forms apache login httpclient

所以我尝试使用Apache的HttpClient v.4登录到一个网站(在这个例子中是myspace)但是我不确定在这个过程中我出错了什么,当我测试这个代码时,Post Login Cookies就是与初始登录cookie相同,但不应该是。

我在网上看了看是否有其他人尝试过,但我找不到一个对我有用的好资源。

我正在使用这个例子:apache 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();}

示例cookie:

    [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]

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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);
    }