title: Java - How to find the redirected url of a url?
body: <p>I am accessing web pages through java as follows:</p>
  
  <pre><code>URLConnection con = url.openConnection();
  </code></pre>
  
  <p>But in some cases, a url redirects to another url. So I want to know the url to which the previous url redirected.</p>
  
  <p>Below are the header fields that I got as a response:</p>
  
  <pre><code>null--&gt;[HTTP/1.1 200 OK]
  Cache-control--&gt;[public,max-age=3600]
  last-modified--&gt;[Sat, 17 Apr 2010 13:45:35 GMT]
  Transfer-Encoding--&gt;[chunked]
  Date--&gt;[Sat, 17 Apr 2010 13:45:35 GMT]
  Vary--&gt;[Accept-Encoding]
  Expires--&gt;[Sat, 17 Apr 2010 14:45:35 GMT]
  Set-Cookie--&gt;[cl_def_hp=copenhagen; domain=.craigslist.org; path=/; expires=Sun, 17     Apr 2011 13:45:35 GMT, cl_def_lang=en; domain=.craigslist.org; path=/; expires=Sun, 17 Apr 2011 13:45:35 GMT]
  Connection--&gt;[close]
  Content-Type--&gt;[text/html; charset=iso-8859-1;]
  Server--&gt;[Apache]
  </code></pre>
  
  <p>So at present, I am constructing the redirected url from the value of the <code>Set-Cookie</code> header field. In the above case, the redirected url is <code>copenhagen.craigslist.org</code></p>
  
  <p>Is there any standard way through which I can determine which url the particular url is going to redirect.</p>
  
  <p>I know that when a url redirects to other url, the server sends an intermediate response containing a <code>Location</code> header field that tells the redirected url but I am not receiving that intermediate response through the <code>url.openConnection();</code> method. </p>