在使用httpclient的postmethod,返回的状态码总是200,不能返回正确值,所以无法获得登录后的页面,请问该如何解决?代码如下:        PostMethod authpost = new PostMethod("/login.jsp");
        // Prepare login parameters
       
       NameValuePair userid   = new NameValuePair("name", "1");
        NameValuePair password = new NameValuePair("Password", "1");
        authpost.setRequestBody(
          new NameValuePair[] {userid,password});        client.executeMethod(authpost);        System.out.println("Login form post: " + authpost.getStatusLine().toString());
        // release any connection resources used by the method
        authpost.releaseConnection();
        // See if we got any cookies
        // The only way of telling whether logon succeeded is
        // by finding a session cookie
        Cookie[] logoncookies = cookiespec.match(
            LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
        System.out.println("Logon cookies:");
        if (logoncookies.length == 0) {
            System.out.println("None");
        } else {
            for (int i = 0; i < logoncookies.length; i++) {
                System.out.println("- " + logoncookies[i].toString());
            }
        }
        // Usually a successful form-based login results in a redicrect to
        // another url
        int statuscode = authpost.getStatusCode();
        String str=new String();
        System.out.println("bian ma"+str.valueOf(statuscode));
        System.out.println("lisu"+ authpost.getStatusLine().toString());
        if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
            (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
            (statuscode == HttpStatus.SC_SEE_OTHER) ||
            (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
            Header header = authpost.getResponseHeader("location");
            if (header != null) {
                String newuri = header.getValue();
                if ((newuri == null) || (newuri.equals(""))) {
                    newuri = "/";
                }
                System.out.println("Redirect target: " + newuri);
                GetMethod redirect = new GetMethod(newuri);                client.executeMethod(redirect);
                System.out.println("Redirect: " + redirect.getStatusLine().toString());
                // release any connection resources used by the method
                redirect.releaseConnection();
            } else {
                System.out.println("Invalid redirect");
                System.exit(1);
            }
        }

解决方案 »

  1.   

    这是帮助上写的
    All response codes between 300 and 399 inclusive are redirect responses of some form. The most common redirect response codes are:301 Moved Permanently. HttpStatus.SC_MOVED_PERMANENTLY 
    302 Moved Temporarily. HttpStatus.SC_MOVED_TEMPORARILY 
    303 See Other. HttpStatus.SC_SEE_OTHER 
    307 Temporary Redirect. HttpStatus.SC_TEMPORARY_REDIRECT 
    我只有获得300-399之间的数才能重定向网页。所以200是不行的
      

  2.   

    根据帮助上写的,当在代码里提交网页后,httpclient并不能自动跳转。所以,需要根据状态码,来手工获取登录的页面。虽然200是正确的,但手工就不能跳转页面了。
      

  3.   

    你先测试一下跳转啊,然后财找原因为什么是200,首先你要清楚一点,200/401/404等这是HTTP协议返回的状态码,而你所说的301/302/303/307等这是你自定义的状态码,这两种是不同的,所以你要关联起来,不要混为一谈!
      

  4.   

    我知道。我刚才试了httpclient提供的例子,也不能获取提交后的页面。是不是httpclient本身就不能获取登录后的页面呀。我快急死了。麻烦会用httpclient的大哥指教一下。我的qq号是59670012,可直接跟我联系。
      

  5.   

    差一点。我向web服务器提交的用户名和口令已经上去了。但是还有一个返回的头我收不到。我用sun的网站测试到可以收到页面跳转的请求。但别的网站和我自己测试的网站都都收不到返回的头。郁闷死了。现在论坛里的人太水了
      

  6.   

    我现在觉得因为http的状态码是200,被认为一次提交结束。如果返回值是301 Moved Permanently. HttpStatus.SC_MOVED_PERMANENTLY,那么我就可以取得登录后的页面。没有高手出来指导一下吗?