title: How to get absolute url using java or jsoup
body: <p>I am having a textbox and submit button in my jsp page. When submitting this button with some url in textbox, I am getting the response of that url using URLConnection</p>
  
  <pre><code>    String strUrl = request.getParameter("url");
      URL url = new URL(strUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      byte[] encodedBytes = Base64.encodeBase64("root:pass".getBytes());
      String encoding = new String(encodedBytes);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("GET");
      connection.connect();
  
      InputStream content = (InputStream) connection.getInputStream();
      BufferedReader in = new BufferedReader(new InputStreamReader(content));
      try {
          fWriter = new FileWriter(new File("f:\\new.html"));
          writer = new BufferedWriter(fWriter);
          while ((line = in.readLine()) != null) {
              String s = line.toString();
              writer.write(s);    
          }               
          writer.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>In the resulting html page, every css and js and images were missing as they are pointed to get from local.
  for example, js is placed as followed in my generated html page.</p>
  
  <pre><code>    &lt;script src="/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>But this actual src is as follows,</p>
  
  <pre><code>    &lt;script src="https://www.url.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>I know that there are many solution to replace all src, href with url host. Found many answers related to that.</p>
  
  <p>I used a solution as follows,</p>
  
  <pre><code>    if (s.contains(("href="))) {
          if (s.contains("\"../") || s.contains("\"/")) {
                      s = s.replace("\"../", "\"http://" + url.getHost() + "/");
                      s = s.replace("\"/", "\"http://" + url.getHost() + "/");
                      writer.write(s);
                      out.println(s);
          }
      }
  </code></pre>
  
  <p>Now I am able to get link,but its not useful in all the web sites. which means that it will helpful for only sites having that kind of host only prefix with src and hrefs.</p>
  
  <p>In some websites, links are defined as href="frmArticles.aspx". In this case its not enough to add host with href url, because href and src are different even though I prefix with host. For example, folowing URL having href links as different than its URL.</p>
  
  <p><a href="http://www.nakkheeran.in/Users/frmMagazine.aspx?M=2" rel="nofollow">http://www.nakkheeran.in/Users/frmMagazine.aspx?M=2</a></p>
  
  <pre><code>    &lt;a href="frmArticles.aspx?A=25744"&gt;தை தை தை&lt;/a&gt;
  </code></pre>
  
  <p>If, I am adding host to this href it becomes as follows,</p>
  
  <pre><code>    &lt;a href="http://www.nakkheeran.in/frmArticles.aspx?A=25744"&gt;தை தை தை&lt;/a&gt;
  </code></pre>
  
  <p>And this is not available. Because, the actual url is </p>
  
  <pre><code>    &lt;a href="http://www.nakkheeran.in/Users/frmArticles.aspx?A=25744"&gt;தை தை தை&lt;/a&gt;
  </code></pre>