就是想通过java.net.下的类去模拟登录WEB上的一些站点,如开心网之类的,先不用考虑验证码,目前只想通过用户 名和密码去登录 ,我写的代码一直不能登录成功,请高手指点一下。或好成功的例子也请转一下,谢谢!分不多,200

解决方案 »

  1.   

    http://hi.baidu.com/dl88250/blog/item/b172a744194dce83b3b7dc4e.html
      

  2.   

    http://hi.baidu.com/dl88250/blog/item/b172a744194dce83b3b7dc4e.html
    这个很全
      

  3.   

    楼主我这里是用Struts1做的一个简单示例,你也完全可以按照自己的框架去实现,这里只是一种思路:public ActionForward login(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    IUserService userService = (IUserService)SpringHelper.getBean("UserService");
    IFunctionService functionService = (IFunctionService)SpringHelper.getBean("FunctionService");
    String loginName = request.getParameter("userName");
    String passWord = request.getParameter("password");
    List isLogin = userService.loginPrm(loginName, passWord);

    if(isLogin.size()==0){
    //用户名或者密码错误,禁止登陆
    request.setAttribute("errors","用户名或者密码错误!");
    return mapping.findForward("relogin");
    }else{
    //用户名和密码正确,可以登陆

    //判断该用户是否被禁用
    String isDeleted = ((UserPO)isLogin.get(0)).getIsdeleted();
    if(isDeleted==null||"1".equals(isDeleted)){
    //该用户已经被禁用
    request.setAttribute("errors","用户已被禁用!");
    return mapping.findForward("relogin");
    }else{
    //没有被禁用,可以正常登陆
    List list = userService.loginPrm(loginName, passWord, "0");
    //获得功能权限
    String functionStr = ((UserPO)list.get(0)).getFunctionPower();
    //获得操作权限
    String operateStr = ((UserPO)list.get(0)).getOperatePower();
    //获得userId
    String userId = ((UserPO)list.get(0)).getUserId();

    LoginInfo logininfo = new LoginInfo();
    logininfo.setUserId(userId);
    logininfo.setFunctionStr(functionStr);
    logininfo.setOperateStr(operateStr);

    //查找所有的功能菜单
    List<FunctionPO> functionPO =functionService.getAllFunctionPO();
    List removeId = new ArrayList();
    //遍历所有的功能菜单列表,如果用户现有的权限和当前存在的所有的功能菜单进行匹配,如果匹配不成功的Id则当前用户不拥有此功能菜单
    if(functionStr!=null&&!"".equals(functionStr)){
    for(FunctionPO fp : functionPO){
    if(functionStr.indexOf(","+fp.getFunctionId()+",")==-1){
    removeId.add(fp);
    }
    }
    //删除当前用户所不能拥有的功能菜单ID
    functionPO.removeAll(removeId);
    request.setAttribute("functionPO", functionPO);
    }

    request.getSession().setAttribute("logininfo", logininfo);

    }
    return mapping.findForward("success");
    }
      

  4.   

    楼主可以去看一下 Apache HttpComponents 项目,下载回来的发布包中有个 ClientFormLogin.java 的例子。这个类在:httpcomponents-client-4.0.1\examples\org\apache\http\examples\client 目录中。