url redirection - How can I calculate the number of times a webpage redirects in java? -
i have tried using following code, it's not working. please help.
httpurlconnection con = (httpurlconnection)(new url(url).openconnection()); con.setinstancefollowredirects( false ); con.connect(); int responsecode = ((httpurlconnection) con).getresponsecode(); int numberhops =0; while (responsecode!=200) { string newurl = con.getheaderfield("location"); httpurlconnection conn = (httpurlconnection) new url(newurl).openconnection(); con.setinstancefollowredirects( false ); responsecode = conn.getresponsecode(); numberhops++; } system.out.println(numberhops);
Comments
Post a Comment