Comment auto redirection en HttpClient (java, apache)

Je créer httpClient et définir les paramètres

HttpClient client = new HttpClient();

client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.getParams().setContentCharset("UTF-8");

Première requête (get)

GetMethod first = new GetMethod("http://vk.com");
int returnCode = client.executeMethod(first);

BufferedReader br = null;
String lineResult = "";
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
    System.err.println("The Post method is not implemented by this URI");
    //still consume the response body
    first.getResponseBodyAsString();
} else {
    br = new BufferedReader(new InputStreamReader(first.getResponseBodyAsStream(), Charset.forName("windows-1251")));
    String readLine = "";
    while (((readLine = br.readLine()) != null)) {
        lineResult += readLine;
    }
}

Réponse correcte.

Deuxième demande (post):

PostMethod second = new PostMethod("http://login.vk.com/?act=login");

second.setRequestHeader("Referer", "http://vk.com/");

second.addParameter("act", "login");
second.addParameter("al_frame", "1");
second.addParameter("captcha_key", "");
second.addParameter("captcha_sid", "");
second.addParameter("expire", "");
second.addParameter("q", "1");
second.addParameter("from_host", "vk.com");
second.addParameter("email", email);
second.addParameter("pass", password);

returnCode = client.executeMethod(second);

br = null;
lineResult = "";
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
    System.err.println("The Post method is not implemented by this URI");
    //still consume the response body
    second.getResponseBodyAsString();
} else {
    br = new BufferedReader(new InputStreamReader(second.getResponseBodyAsStream()));
    String readLine = "";
    while (((readLine = br.readLine()) != null)) {
        lineResult += readLine;
    }
}

cette réponse est correcte aussi, mais j'ai besoin d'être redirigé vers les en-Têtes.Emplacement.

Je ne sais pas comment obtenir la valeur des en-Têtes Emplacement ou comment activer automatiquement la redirection.

OriginalL'auteur Mediator | 2011-04-30