SetRequestProperty (Showing top 20 results out of 12,087). URLConnection connection = new URL(url). Setting user agent of a java URLConnection. SetRequestProperty('User-Agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4;. Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types, and session cookies. Optionally upload a.
The
HttpURLConnection
‘s follow redirect is just an indicator, in fact it won’t help you to do the “real” http redirection, you still need to handle it manually.
1. Java Http Redirect Example
If a server is redirected from the original URL to another URL, the response code should be 301: Moved Permanently or 302: Temporary Redirect. And you can get the new redirected url by reading the “Location” header of the HTTP response header.
For example, access to the normal HTTP twitter website – http://www.twitter.com , it will auto redirect to the HTTPS twitter website – https://www.twitter.com.
HttpRedirectExample – Full Java follow redirect example, see comments for self-explanatory.
Output
Do share if you have better way to handle the HTTP redirection 🙂