0

there are a lot of examples how to read url page content with java client. For example here is with apache http client ( http://hc.apache.org/httpclient-legacy/tutorial.html)

HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
   System.err.println("Method failed: " + method.getStatusLine());
}
byte[] responseBody = method.getResponseBody();

Here is my question: In page url can be redirect to other url after some time. For example in url www.mysite.com/xxx there is redirect after 5sec from javascript to url www.mysite.com/realpage/xxx, but you can not go directly to www.mysite.com/real-page/xxx, only with redirect.

<script type="text/javascript">
    function go() {
        document.location.href = "http://www.mysite.com/realpage/xxx";
    }
window.setTimeout("go()",5000);
</script>

How to do get this redirect in java client, and how to get content of this page in java client ? Tnx !

prilia
  • 996
  • 5
  • 18
  • 41
  • 1
    Have you checked this out? http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect – Raji Jul 29 '13 at 12:08

1 Answers1

2

This isn't a redirect, this is JavaScript in the brower navigating to another page. If the browser can see the other page, so can your application, firewalls and proxies permitting. So your code can simply load the other URL.

However, if you mean, given a response containing this piece of JavaScript, how can you programatically run the code in the script element, then that's a whole lot harder.

While running JavaScript in Java is straight forward enough using the javax.script API, running it with a complete DOM and handling changes to that DOM is a whole lot harder.

While it might be an interesting exercise to write an engine with its own DOM, my advice would be to use an API like the Selenium WebDriver that has already done it for you.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58