2

我想用 HTTPClient() 登录一个网站;而且我无法遵循重定向(我有一个例外)。

我创建了我的客户端和 hostConfig :

 client = new HttpClient();
        client.getParams().setParameter(HttpMethodParams.USER_AGENT, useragent);
        client.getParams().setParameter(HttpMethodParams.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        client.getParams().setParameter("http.protocol.allow-circular-redirects", true);
        client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

[...]

HostConfiguration hostConfig = client.getHostConfiguration();
                hostConfig.setHost(new URI(url, true));

                PostMethod post = new PostMethod(login);
                post.addParameter("username", pseudo);
                post.addParameter("password", password);
                post.addParameter("secure_login", tooken);

 getPage(client, post, hostConfig);

在我的 GetPage 方法中,我执行以下重定向:

   public String getPage(HttpClient myClient, PostMethod myMethod, HostConfiguration hostConfig) {

            [...]

            try {
                // execute methode
                int returnCode = myClient.executeMethod(hostConfig, myMethod);


                // handle redirection
                if (returnCode == HttpStatus.SC_MOVED_TEMPORARILY
                        || returnCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || returnCode == HttpStatus.SC_SEE_OTHER
                        || returnCode == HttpStatus.SC_TEMPORARY_REDIRECT) {

                    System.out.println("Redirection encountered:" + returnCode);

                    String redirectLocation = null;
                    Header locationHeader = myMethod.getResponseHeader("location");
                    if (locationHeader != null) {
                        redirectLocation = locationHeader.getValue();

                    } else {
                        System.err.println("ERROR : 01");
                    }

                    if (redirectLocation != null) {
                        System.out.println("redirectLocation : "+redirectLocation);
                        hostConfig.setHost(new URI(url + redirectLocation, true));
                       GetMethod redirect = new GetMethod(url + redirectLocation);
                returnCode = myClient.executeMethod(hostConfig, redirect);
                    }

                }
                // Handle redirects

                else if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                  [...]
                } else {
                          // get Result in String
                    }
                }
            } catch (Exception e) {
                System.err.println(e);
            } finally {
                myMethod.releaseConnection();
                if (br != null) {
                    try {
                        br.close();
                    } catch (Exception fe) {
                    }
                }
            }

            return retval;

        }

我的外壳结果:

Redirection encountered:302
redirectLocation : /?section=INDEX
juil. 20, 2013 12:16:07 PM org.apache.commons.httpclient.HttpMethodDirector isRedirectNeeded
INFO: Redirect requested but followRedirects is disabled

我如何关注重定向?

PS:如果我使用:

myMethod.setFollowRedirects(true);

我还有一个例外:

java.lang.IllegalArgumentException: Entity enclosing requests cannot be redirected without user intervention
4

0 回答 0