jordanhgl
jordanhgl
2010-03-25 14:03
浏览 255
已采纳

httpclient 实现模拟登陆的问题

我想用httpclient 做一个摸模拟登陆人人网(就是类似的网站,要输入用户名密码的那种,论坛啦网友啦)的应用程序,我知道用httpclient的post方法实现向服务器提交参数(账户名和密码)的方法,可是接下来我要怎样才能知道我是否登陆成功了呢?如果登录成功就放会“登陆成功!”反之则返回“登录失败!”我要如何通过服务器返回来的信息获得是否登录成功的信息呢?希望给位大侠能不吝赐教啊!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • wanghaolovezlq
    wanghaolovezlq 2010-03-25 14:20
    已采纳

    httpclient 模拟登陆网站 获取网站内容程序
    package org.apache.http.examples.client;

    import java.io.IOException;

    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.cookie.*;
    import org.apache.commons.httpclient.methods.*;

    public class FormLoginDemo {
    static final String LOGON_SITE = "www.intellitrans.com.cn";
    static final int LOGON_PORT = 8081;
    static final String loginurl = "/zyzg/logon.do";
    static final String loginparematername = "userName";
    static final String loginparematerpass = "password";
    static final String username = "wangpx";
    static final String password = "111111";

    static final String getUrl = "/zyzg/hrmsub/hrm-deptlist.do";
    
    
    public static void main(String[] args) throws Exception {
        HttpClient client = imitateLogin(LOGON_SITE, LOGON_PORT, loginurl, loginparematername, loginparematerpass, username, password);
        // 访问所需的页面
        imitateGetUrl(client, getUrl);
    }
    
    //模拟等录 
    private static HttpClient imitateLogin(String LOGON_SITE, int LOGON_PORT,
            String loginurl,String loginparematername,String loginparematerpass,String username,String password) throws IOException, HttpException {
        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
        // 模拟登录页面
        PostMethod post = new PostMethod(loginurl);
        NameValuePair name = new NameValuePair(loginparematername,username );
        NameValuePair pass = new NameValuePair(loginparematerpass,password );
        post.setRequestBody(new NameValuePair[] { name, pass });
        int status = client.executeMethod(post);
        System.out.println(post.getResponseBodyAsString());
        post.releaseConnection();
        // 查看cookie信息
        CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
        Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false,
                client.getState().getCookies());
        if (cookies != null)
            if (cookies.length == 0) {
                System.out.println("None");
            } else {
                for (int i = 0; i < cookies.length; i++) {
                    System.out.println(cookies[i].toString());
                }
            }
        return client;
    }
    //模拟等录 后获取所需要的页面
    private static void imitateGetUrl(HttpClient client, String getUrl)
            throws IOException, HttpException {
        PostMethod post2 = new PostMethod(getUrl);
        // GetMethod get = new
        // GetMethod("/social/article/gallery_show/p_104846/");
        client.executeMethod(post2);
        System.out.println(post2.getResponseBodyAsString());
        post2.releaseConnection();
    }
    

    }

    点赞 评论
  • iteye_13500
    iteye_13500 2010-03-25 14:06

    [quote]如果登录成功就放会“登陆成功!”反之则返回“登录失败![/quote]

    如果登录成功,会返回200响应码的,失败会返回其它响应码。

    点赞 评论
  • suziwen
    suziwen 2010-03-25 14:12

    通过HTML页面的值,

    你可以先在浏览器里测试登录成功与登录失败返回的页面

    在查看源文件里
    找出登录成功的HTML与登录失败的HTML有哪段可以区分开来的代码。

    拿出这段代码。

    放到你的JAVA代码里,

    当你用JAVA登录后,应该会返回一个流,
    你把流里的内容转成STRING或其他,
    通过判断是否包含你所COPY的代码来判断登录成功与否

    点赞 评论
  • qwe_rt
    qwe_rt 2010-03-25 14:33

    我来讲讲,登录失败后,返回的字符流是人人网的登录页面源码。
    第一,登录成功后,返回的肯定是个人首页html代码。
    第二,登录失败后,首先我们想想,在网页(renren.com)上登录,我们清楚登录失败后页面跳转到[color=red]带有错误提示[/color]的登录页面,说明什么?说明返回的字符流就是“带有错误提示的登录页面”。
    现在明白了,返回时什么了吧,

    然后来测试一下,下面使用的是c#代码测试(java代码上面有);

    下面是我的测试代码:

    [code="c#"]/////////////////////////////////////////////
    //自动填充登陆信息,并提交
    //人人网登陆
    /////////////////////////////////////////////
    string email =163@163.com;//用户名样例
    string password = 12345;//密码样例
    string autoLogin = "true";
    string origURL = "http://www.renren.com/Home.do";
    string domain = "renren.com";
    //将文本转换为URL编码字符串
    email = System.Web.HttpUtility.UrlEncode(email);
    password = System.Web.HttpUtility.UrlEncode(password);
    autoLogin = System.Web.HttpUtility.UrlEncode(autoLogin);
    origURL = System.Web.HttpUtility.UrlEncode(origURL);
    domain = System.Web.HttpUtility.UrlEncode(domain);

            //格式要提交的字符串数据
            string formatString = "email={0}&password={1}&autoLogin={2}&origURL={3}&domain={4}";
            string postString = string.Format(formatString, email, password, autoLogin,origURL,domain);
    
            //将提交的字符串数据转换成字节数组
            byte[] postData = Encoding.ASCII.GetBytes(postString);
    
            //设置提交的相关参数
    
            string URI = "http://www.renren.com/PLogin.do";
            HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
            request.Method = "POST";
            request.KeepAlive = false;
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookieContainer;
            request.ContentLength = postData.Length;
    
            // 提交请求数据
            System.IO.Stream outputStream = request.GetRequestStream();
            outputStream.Write(postData, 0, postData.Length);
            outputStream.Close();
    
            // 接收返回的页面
            HttpWebResponse response;
            response = request.GetResponse() as HttpWebResponse;
           System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
           string srcString = reader.ReadToEnd();
    
           Console.Write(srcString+"测试返回代码");
    

    [/code]

    测试结果:返回就是带有错误提示的登录页面代码。

    点赞 评论

相关推荐