我想做个J2SE登陆到WEB的程序,但是登陆帐号密码核对完后,我就被WEB程序给T回了程序,我登陆的地方是后台,我想实现用J2SE对WEB后台操作,请不要帖下面的代码:URL   url   =   new   URL("http://localhost/admin/chklogin.asp");
        HttpURLConnection   conn   =   (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        
        OutputStream   raw   =   conn.getOutputStream();
        OutputStream   buf   =   new   BufferedOutputStream(raw);
        OutputStreamWriter   out   =   new   OutputStreamWriter(buf,"GBK");
        out.write("admin=admin&password=admin");
        out.flush();
        out.close();
        conn.connect();
        
        InputStream in=conn.getInputStream();
        in=new BufferedInputStream(in);
        Reader r=new InputStreamReader(in);
        
        int   c;
        //int i=0;
      
            
             byte[] b=new byte[1800];   
             int j=0;
              while(true)   
              {   
                      int i=in.read();   
                      if(i==-1)   break;   
                      b[j++]=(byte)i;
              }   
             
             System.out.println(new String(b));这段代码,就是我用过的。如果我登陆失败,会跳到登陆失败的界面,成功后转入admin.asp<%
  if session("aleave")="" then
      response.redirect "adminlogin.asp"
  response.end
  end if
%>我就被这代码T回来的。密码核对成功后转到admin.asp,这是admin.asp的头部代码session("admin")=rs("admin")
session("password")=rs("password")
session("aleave")=rs("aleave")
response.redirect "admin.asp"但是到了admin.asp我会被T回来希望大家解决一下,我高分酬谢

解决方案 »

  1.   

    建议楼主使用HttpUnit,如果有问题发站内短信给我,我前不久刚刚实现了你说的类似功能
      

  2.   

    J2SE  里面  有 HttpUnit 这个类?
      

  3.   

    HttpUnit是个第三方的包
    http://httpunit.sourceforge.net/
    做你要的这个功能很简单就可以实现
      

  4.   

    第三方包,不也是用JAVA 写出来的,我想要用JAVA怎么实现,而不想用第三包,
      

  5.   

    在返回的http head里面取cookie里的sessionid 第二次请求是sessionid带上
      

  6.   

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    public class TestHTTP {
        public TestHTTP() {
            
        }
        public static void main(String[] argv) throws Exception {
            URL url = new URL("http://localhost/admin/CHKLOGIN.ASP");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);        PrintWriter out = new PrintWriter(connection.getOutputStream());    // encode the message
            String name = "admin="+URLEncoder.encode("admin","UTF-8");
            String email = "password="+URLEncoder.encode("admin","UTF-8");    // send the encoded message
            out.println(name+"&"+email);
            out.close();
            java.util.Map map=connection.getHeaderFields();
            java.util.Iterator it=map.keySet().iterator();
            
            while(it.hasNext()) {
                Object key=it.next();
                System.out.println("key="+key+"  value="+map.get(key));
            }
            
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                //System.out.println(line);
            }
            System.out.println("----------------------");
            in.close();
            connection.setFollowRedirects(true);
            String cookie=connection.getHeaderField("Set-Cookie").toString().substring(0,connection.getHeaderField("Set-Cookie").toString().indexOf(";"));
            url=new URL("http://localhost/admin/CHKLOGIN.ASP");
            connection=(HttpURLConnection)url.openConnection();
            connection.setRequestProperty("Cookie",cookie);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setInstanceFollowRedirects(true);
            out = new PrintWriter(connection.getOutputStream()); 
            name = "admin="+"admin";
            email = "password="+"admin";    // send the encoded message
            out.println(name+"&"+email);
            System.out.println(name+"&"+email);
            out.flush();
            out.close();
            System.out.println(connection.getRequestProperty("Cookie"));
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
        }
        
    }
    我现在代码是这样的,但是还是不行
      

  7.   

    我用VC++实现了此功能,但是只是一般的,像这个www.ename.cn
    我在浏览器打开url+数据都登录不了,也不知道是什么安全机制
    表示关注,建议重发帖子,分高点
      

  8.   

    我也觉得是session的问题,你可以用TCPMonitor(google一下,很容易找到的,很好用)看看正常的和你发送的数据有什么不同。
      

  9.   

    感觉上你可以测试一下在你登陆的页面把验证的过程在后台的webserver里打印出来看看是否登陆成功。
    或者试试把url改成
    url=new URL("http://localhost/admin/CHKLOGIN.ASP?admin="+admin+"&password="+password);
      

  10.   

    ...楼上的,我都测试过了,连代码测试是否有数据进入,我都给输出来了,问题是,就是不给我保存SESSION,转到其他页面就丢失了
      

  11.   

    接上贴    /**
         * 处理进程
         */
        public String process(String phone, String pwd, String songid) throws
                SAXException,
                MalformedURLException, IOException {
            info1.setText("正在登陆");
            URL url = null;
            String v = "";
            WebConversation wc = new WebConversation();        WebResponse wr = wc.getResponse(loginPage.getText());        String str = wr.getText();
            int start = str.indexOf("<input type=\"text\" style=\"width:40\" name=\"randompwd\" value=\"\" maxlength=\"6\" class=\"input-style2\" >") +
                        "<input type=\"text\" style=\"width:40\" name=\"randompwd\" value=\"\" maxlength=\"6\" class=\"input-style2\" >".
                        length();
            int end = str.indexOf("</td>", start);
            str = str.substring(start, end).trim();
            String[] validate1 = str.split(".gif\">");        for (int i = 0; i < validate1.length; i++) {
                validate1[i] = validate1[i].trim();
                v = v +
                    validate1[i].substring(validate1[i].length() - 1,
                                           validate1[i].length());
            }
            WebForm wf = wr.getForms()[0];
            String p[] = wf.getParameterNames();        WebRequest request = wf.getRequest();
            request.setParameter("areanumber", "0731");
            request.setParameter("callnumber", phone);
            request.setParameter("password", pwd);
            request.setParameter("randompwd", v);
            String s = "document.inputForm.usernumber = '"+ quhao.getText() + phone + "';";
            wr.getScriptableObject().doEvent(s);
            s = "document.inputForm.cardpass = '" + pwd + "';";
            wr.getScriptableObject().doEvent(s);
            s = "document.inputForm.ipaddr = '" + randomIP() + "';";
            wr.getScriptableObject().doEvent(s);
            s = "document.inputForm.onlyflag = '0';";
            wr.getScriptableObject().doEvent(s);
            wf.submit();        info1.setText("正在处理");
            try {
                Thread.sleep(400);
            } catch (InterruptedException ex1) {
            }        wr = wc.getCurrentPage();        String in = wr.getText();
            if (in.indexOf("用户不存在") != -1 || in.indexOf("您输入的密码不正确") != -1) {
                info1.setText("用户名或者密码不正确");
                try {
                    Thread.sleep(400);
                } catch (InterruptedException ex1) {
                }
                return "用户名或者密码不正确";
            }
            HttpUnitOptions.setExceptionsThrownOnScriptError(false);
            HttpUnitOptions.getScriptErrorMessages();
            wr = wc.getResponse(
                    "http://ring.hnphs.net/colorring/buyring.action?ringidtype=1&ringid=" +
                    songid + "&grouptype=-1&scorebuy=0");
            in = wr.getText();
            if (in.indexOf("用户已经拥有这条铃音") != -1) {
                info1.setText("用户已经拥有这条铃音");
                try {
                    Thread.sleep(400);
                } catch (InterruptedException ex1) {
                }
                //删除铃声
                info1.setText("正在删除用户已经订购的铃声");
                wr = wc.getResponse("http://ring.hnphs.net/colorring/initeditring.action");
                s = "document.personringlibtable.action='/colorring/delpersonring.action?ringid="+songid+"&ringidtype=1';";
                wr.getScriptableObject().doEvent(s);
                wf = wr.getFormWithName("personringlibtable");
                wr = wf.submit();
                return "铃声已经删除";
            }        info1.setText("铃音订购成功");
            try {
                Thread.sleep(400);
            } catch (InterruptedException ex1) {
            }
            return "铃音订购成功";
        }    /**
         * 随机产生一个IP地址
         * @return String 产生的IP地址字符串
         */
        public String randomIP() {
            Random rand = new Random();
            String str = "";
            for (int i = 0; i < 4; i++) {
                str = str + "." + (rand.nextInt(20) + 225);
            }
            str = str.substring(1, str.length());
            System.out.println(str);
            return str;
        }    /**
         *  弹出一个消息提示
         * @param msg String 要显示的内容
         */
        public void showMessage(String msg) {
            jButton1.setEnabled(true);
            fileNameField.setEditable(true);
            jButton2.setEnabled(true);
            jButton2.setText("开始");
            JOptionPane.showMessageDialog(null, msg);    }
    }
    class Frame1_jButton2_actionAdapter implements ActionListener {
        private Frame1 adaptee;
        Frame1_jButton2_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }    public void actionPerformed(ActionEvent e) {
            adaptee.jButton2_actionPerformed(e);
        }
    }
    class Frame1_jButton1_actionAdapter implements ActionListener {
        private Frame1 adaptee;
        Frame1_jButton1_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }    public void actionPerformed(ActionEvent e) {
            adaptee.jButton1_actionPerformed(e);
        }
    }这个我是写的一个登陆网站并进行了一个操作的代码,用HttpUnit实现的
      

  12.   

    有没有不用 别人写的 类,用JAVA 本身自己的类来实现的