I'm trying to open a web page to automate some data checking, and I'm getting a "too many redirects" exception. I'm not experienced enough with Java to know what to try next, and would like some help.
I'll show the code for what I've tried, but first, details about the website:
http://courts.dallascounty.org
The website is a series of aspx pages. Going to the above address in a browser gets you to "default.aspx"--a search page. You don't see the redirects (specifically to a Login.aspx page--which redirects
back to default.aspx). I can get the search page with either Firefox or wget--and on the same computer that I'm writing the Java code on. So, Firefox and wget are doing something that my code isn't. Also, I'm not behind a proxy.
I've searched about the redirect exception, and one or two pages blame poor website development. That may be true, but I don't have any control over the website. So, fixing the website
is not an option.
I did find
this page where someone has the same issue. I used some code given in one of the responses to discover the default.aspx->Login.aspx->default.aspx redirection loop.
Much of my code was pulled from the
Working with URLs and
Working with Cookies tutorials from Oracle. I tried adding the cookie handler thinking that maybe Login.aspx was trying to create a session ID or some other connection-specific identifier. But either cookies are not the solution OR I just didn't code it the right way.
So here's the code. It's ugly (in an every-branch-of-the-ugly-tree way):
Code:
package dallasinformationstatus;
import java.io.*;
import java.net.*;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
CookieManager cookieMonster = new CookieManager();
cookieMonster.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieMonster);
List<HttpCookie> cookieJar;
HttpCookie chocolateCookie;
/* Various addresses that end up falling into the redirect loop */
// URI dallasURI = new URI("http://courts.dallascounty.org/");
// URI dallasURI = new URI("http://courts.dallascounty.org/Login.aspx");
URI dallasURI = new URI("http://courts.dallascounty.org/default.aspx");
// URI dallasURI = new URI("http://courts.dallascounty.org/Login.aspx?ReturnUrl=/default.aspx");
URL dallasURL = dallasURI.toURL();
/* Comment the line below to generate the redirect exception.
It is left uncommented here because I was using it to find out where
the code was redirected to for each of the URIs listed above */
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection dallasConnection = null;
try {
dallasConnection = (HttpURLConnection) dallasURL.openConnection();
System.out.println("Response code = " + dallasConnection.getResponseCode());
String header = dallasConnection.getHeaderField("location");
if (header != null)
System.out.println("Redirected to " + header);
BufferedReader in = new BufferedReader(
new InputStreamReader(
dallasConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch (Exception e ) {
System.out.println("EXCEPTION: " + e.getMessage() );
if( dallasConnection != null ) {
System.out.println("Response code = " + dallasConnection.getResponseCode());
}
String header = dallasConnection.getHeaderField("location");
if (header != null)
System.out.println("Redirected to " + header);
/* Cookie stuff added here in the vain hope that I would see
SOMETHING being stored by the website to give me some
clue about what to do next, but I never get anything
other than no cookies :( */
cookieJar = cookieMonster.getCookieStore().getCookies();
if( cookieJar.isEmpty() ) {
System.out.println("-->NO<-- cookie!");
} else {
chocolateCookie = cookieJar.get(0);
System.out.println("COOKIE:\n Domain: " +
chocolateCookie.getDomain() + "\n Name: " +
chocolateCookie.getName() + "\n Value: " +
chocolateCookie.getValue() );
}
}
}
}
Like I said, I don't know what else to try. I need to get this done using Java--no shell scripts, no calls to wget within Java, etc. Sample code would be great, but pointing me to some documentation would be just as good.
Thanks for any help (and for reading this wall of text).
EDIT:
After more searching, I'm 99% certain it's a cookie-handling issue. I added some more code (not included in the above) that examines the full response from the redirect to the Login.aspx page. The response includes a Set-Cookie header for "ASP.NET_SessionId". Now to find some code that will store and send the session ID.