title: How to get replaced/redirected URL by java code
body: <p>When parsing a webpage, I get the link href=<a href="http://www.onvista.de/aktien/snapshot.html?ID_OSI=36714349" rel="nofollow">http://www.onvista.de/aktien/snapshot.html?ID_OSI=36714349</a>
  When issuing this link in my browser, it replaces it with "<a href="http://www.onvista.de/aktien/Adidas-Aktie-DE000A1EWWW0" rel="nofollow">http://www.onvista.de/aktien/Adidas-Aktie-DE000A1EWWW0</a>" and renders it correctly.
  But with java I fail to retrieve the page. I used the following sample which was suggested here to display redirected URLs.  </p>
  
  <pre><code>import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  
  public class GetRedirected {
  
      public GetRedirected() throws MalformedURLException, IOException {
          String url="http://www.onvista.de/aktien/snapshot.html?ID_OSI=36714349";
          URLConnection con = new URL( url ).openConnection();
          System.out.println( "orignal url: " + con.getURL() );
          con.connect();
          System.out.println( "connected url: " + con.getURL() );
          InputStream is = con.getInputStream();
          System.out.println( "redirected url: " + con.getURL() );
          is.close();
      }
  
      public static void main(String[] args) throws Exception {
          new GetRedirected();
      }
  }
  </code></pre>
  
  <p>But it fails at the "InputStream is ="-statement with the attached error message. How may I solve this. Any idea is welcome. </p>
  
  <blockquote>
    <p>orignal url: www.onvista.de/aktien/snapshot.html?ID_OSI=36714349</p>
    
    <p>connected url: www.onvista.de/aktien/snapshot.html?ID_OSI=36714349</p>
    
    <p>Exception in thread "main" java.io.IOException: Server returned HTTP</p>
    
    <p>response code: 403 for URL: www.onvista.de/aktien/snapshot.html?ID_OSI=36714349</p>
    
    <p>at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) </p>
    
    <p>at de.gombers.broker....</p>
  </blockquote>