News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

HttpURLConnection SetFollowRedirects() Example

Started by VelMurugan, Dec 22, 2008, 11:40 PM

Previous topic - Next topic

VelMurugan

HttpURLConnection SetFollowRedirects() Example

Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.

HttpURLConnectionclass SetFollowRedirects() method example. This example shows you how to use SetFollowRedirects() method.This method Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.

Here is the code:-


/*
* @Program that Sets whether HTTP redirects (requests with response code 3xx)
    should be automatically followed by this class.
* SetFollowRedirects.java
* Author:-RoseIndia Team
* Date:-25-Jun-2008
*/

import java.io.*;
import java.net.*;

public class SetFollowRedirects {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // Returns  whether HTTP redirects should be automatically followed.
        System.out.println("HTTP redirects should be automatically followed: "
                +connection.getFollowRedirects());
        connection.setFollowRedirects(false);
        System.out.println("HTTP redirects should be automatically followed: "
                +connection.getFollowRedirects());
    }
}


Output of the program:-

HTTP redirects should be automatically followed: true
HTTP redirects should be automatically followed: false

Source : codingdiary