Java: Capture URL redirects
URLs are generally aliases that are then redirected to another URL. There are times when you actually want to caputre the URL itself rather than be redirected automatically. A simple example of this is when redirects lead to a file download, where you potentially need to handle the file download code differently or do different actions based on the file type etc.
In order to capture the redirected URL, the code snippet below demonstrates how to do this:
The “magic” here happens in these 2 steps:
By default InstanceFollowRedirects
is set to true, but you want to set it to false in order to capture the second URL. Then to retrieve that second URL from the first URL, you need to get the value of the header field called Location
from the first URL connection.