Get hold of redirect url with Java org.apache.http.client

14,062

Solution 1

See my answer to this question,

HttpClient 4 - how to capture last redirect URL

Solution 2

I am assuming that you want to automate browser operations and maintain session so that you can access those pages too which need session to be maintained.

I don't know how to this through org.apache.http.client API. If you are not restricted to use org.apache.http.client API and can use other API then you can use HtmlUnit API otherwise you can ignore the rest of the answer.

Maintaining sessions and automating browser operations through HtmlUnit can be done as follows:

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

final WebClient webClient = new WebClient();
    try {
        webClient.setJavaScriptEnabled(true);
        webClient.setThrowExceptionOnScriptError(false);
        webClient.setCssEnabled(true);
        webClient.setUseInsecureSSL(true);
        webClient.setRedirectEnabled(true);

        HtmlPage loginPage = webClient.getPage(new URL("https://www.orkut.com/"));
        System.out.println(loginPage.getTitleText());
        List<HtmlForm> forms = loginPage.getForms();
        HtmlForm loginForm = forms.get(0);
        HtmlTextInput username = loginForm.getInputByName("Email");
        HtmlPasswordInput password = loginForm.getInputByName("Passwd");
        HtmlInput submit = loginForm.getInputByName("signIn");
        username.setValueAttribute("username");
        password.setValueAttribute("password");
        HtmlPage homePage = submit.click();.
        Thread.sleep(10 * 1000);
        HtmlPage homePageFrame = (HtmlPage) homePage.getFrameByName("orkutFrame").getEnclosedPage();
        HtmlPage communitiesTestPage = (HtmlPage) webClient.openWindow(new URL("http://www.orkut.co.in/Main#Community?cmm=1"), "CommunitiesWindow").getEnclosedPage();
    }catch(java.security.GeneralSecurityException e) {
        e.printStackTrace();
    }catch(java.io.IOException e) {
        e.printStackTrace();
    }catch(InterruptedException e) {
        e.printStackTrace();
    }

    WebWindow ww = webClient.getWebWindowByName("CommunitiesWindow");
    WebRequestSettings wrs1 = new WebRequestSettings(URL); // URL is the url that requires authentication first

As you can see that how the above code automate browser operations and how it maintains session automatically. We don't need to handle cookies or URLReDirect manually...

Solution 3

There is a simple way I came about in Java. Following are the steps:

  1. Create the HttpUrl connection.
  2. Set HttpURLConnection.setFollowRedirects( true ); // this should be true by default
  3. Call connect over your HttpUrlConnection object;
  4. After connect call the getHeadersFields() over your HttpUrlConnection object;
  5. Get the redirected URL by calling the getUrl() over the above HttpUrlConnection object;

There is also another way of getting it using the Location field in HTTP Headers, but sometimes we do not get the Location field in headers. It did not work for me at least. But the above method, it works for sure.

Share:
14,062
jakob
Author by

jakob

Updated on June 04, 2022

Comments

  • jakob
    jakob almost 2 years

    I need help with figuring out how to get hold of the redirect after I make a post to the server. First, I need to do a get to obtain some cookies from the server. Then I perform a post with the cookies and additional parameters. The server then answers with a 302 redirect. How do I get the url for that redirect?

    Code looks like follows:

    HttpGet get = new HttpGet(urlOne);
    
    try {
        //Creating a local instance of cookie store.
        CookieStore cookieJar = new BasicCookieStore();
    
        // Creating a local HTTP context
        HttpContext localContext = new BasicHttpContext();
    
        // Bind custom cookie store to the local context
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);
    
        HttpResponse response = httpClient.execute(get, localContext);
        HttpEntity entity = response.getEntity();
    
        System.out.println("------------------GET----------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
    
        // Print out cookies obtained from server
        List<Cookie> cookies = cookieJar.getCookies();
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("Local cookie: " + cookies.get(i));
        }        
    
        if (entity != null) {
           entity.consumeContent();
        }
        System.out.println("------------------GET-END---------------------");
    
        // Create a new post
        HttpPost post = new HttpPost(urlTwo);
        post.setHeader("Content-Type", "application/x-www-form-urlencoded");
    
        // Add params
        HttpParams params = new BasicHttpParams();
        params.setParameter("action", "search");
        params.setParameter("word", "hello");
    
        post.setParams(params);
    
        //Execute
        HttpResponse response2 = httpClient.execute(post, localContext);