There are two ways to control redirects in this version of HttpClient. The first way to control redirects is simply by specifying the maximum number of redirects, the HttpClient is allowed to perform.
The second way to control redirects is by using the HttpBehavior class.
This time we will create an “HttpBehavior” class to control the redirects. We will tell it exactly what are steps we expect the HttpClient to perform. Then when we execute the GetMethod (line 12), we pass the HttpBehavior object to the HttpClient as a parameter, and the HttpClient will make sure every request and response cycle matches exactly the behavior we expect.
This example will send a GET request to “http://www.codescales.com/home”, and it will expect to receive a 301 response with “http://www.codescales.com” as the new location. Then it will send another GET request to the new location, and it will expect the response to have a 200 status code.
Any deviation from this behavior will result in an HttpBehaviorException thrown.