java 利用 httpClient 模拟登陆网站时,网站重定向,我该怎么进入后继的网页啊!

我用的是httpclient4.3.6 。我模拟登陆我们学校的网站,获取了cookies,但是我用httpwatch看 有重定向的问题,我不知道怎么进入后面的网页!求解!我贴上代码,求代码详解,谢谢!!!小弟初学java 现需要做这个操作,很是艰难呀!感激不尽!

public class Test {

public static void main(String[] args) throws Exception {
    BasicCookieStore cookieStore = new BasicCookieStore();
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCookieStore(cookieStore)
            .build();
    try {
        HttpUriRequest login = RequestBuilder.post()
                .setUri(new URI("http://portal.sicnu.edu.cn:82/cas/login"))
                .addParameter("action","DCPLogin")
                .addParameter("encodedService","http%3a%2f%2fportal.sicnu.edu.cn%2fcas.jsp")
                .addParameter("lt","LT_M5000-R_-592543-IhH4ao22pYE5PM83cHXa")
                .addParameter("password", "123456")
                .addParameter("service","http://portal.sicnu.edu.cn/cas.jsp")
                .addParameter("serviceName","null")
                .addParameter("username", "2013110401")
                .build();
        String MainURL = "http://portal.sicnu.edu.cn/portal/media-type/html/role/school/page/default.psml/js_pane/P-132fb077a30-1000d";

        CloseableHttpResponse response2 = httpclient.execute(login);
        try {
            HttpEntity entity = response2.getEntity();
            System.out.println("Login form get: " + response2.getStatusLine());
            EntityUtils.consume(entity);
            System.out.println("Post logon cookies:");
            List<Cookie> cookies = cookieStore.getCookies();

            if (cookies.isEmpty()) {
              System.out.println("None");
            } else {
                for (int i = 0; i < cookies.size(); i++) {
                    System.out.println("- " + cookies.get(i).toString());
               }
            }finally{
                                   response2.close();
                            }
                            }finally{
                                  httpclient.close();
                            }

httpwatch 看到的

httpclient在登录的时候,你要做个处理,检测返回的状态吗不是200,如果是300系列,你的代码需要模拟浏览器的行为,根据返回的location地址,浏览器重新
向location指向的地址发送请求。这个就是重定向,浏览器也是这么干的,浏览器默认重定向一次最多支持5次重定向。

httpclient实现登录过程中会生成一个client对象,只要不关闭这个对象,client对象会记录这个登陆网站的临时session id。你可以重用这个client post或者get操作到你的目标url。