Results 1 to 6 of 6
  1. #1
    Steel_Bird is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Blank “Location” header in HttpURLCONNECTION but redirect happens in IE

    I am trying to access a URL with HttpURLCONNECTION object, but the redirect just does not seem to be identified because the Location header is blank. Is there any other way I can follow the redirects. setFollowRedirects also is not helping.

    I am trying to test this for an internal application, but another public URL that I found was behaving the same is www0.bankofamerica.com/home-loans/mortgage-purchase.go
    Somehow the below code is not able to create the connection and neither does it create a response code. It works fine for other URL's. Please Help.

    private static boolean isLive(String link) {
    HttpURLConnection urlConnection = null;
    try {
    URL url = new URL(link);
    System.out.println(url);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setInstanceFollowRedirects(true);
    urlConnection.setRequestMethod("GET");
    // urlConnection.connect();

    //urlConnection.setConnectTimeout(5000); /* timeout after 5s if can't connect */
    //urlConnection.setReadTimeout(5000); /* timeout after 5s if the page is too slow */
    urlConnection.connect();
    int resp = urlConnection.getResponseCode();
    System.out.println(resp);
    String redirectLink = urlConnection.getHeaderField("Location");
    System.out.println(redirectLink);
    if (redirectLink != null && !link.equals(redirectLink)) {
    return isLive(redirectLink);
    } else {
    return urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK;
    }
    } catch (Exception e) {
    return false;
    } finally {
    if (urlConnection != null) {
    urlConnection.disconnect();
    }
    }

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Blank “Location” header in HttpURLCONNECTION but redirect happens in IE

    Niether IE and firefox can get to the www0.bankofamerica.com/home-loans/mortgage-purchase.go, so its not a problem with program.

    Maybe this might help:
    Java - How to find the redirected url of a url? - Stack Overflow

  3. #3
    Steel_Bird is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Blank “Location” header in HttpURLCONNECTION but redirect happens in IE

    I'm guessing its redirecting to another URL in IE or FF.. but the same URL does not throw a 301 error on using the above code. Any thoughts on that ?

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Blank “Location” header in HttpURLCONNECTION but redirect happens in IE

    I have to admit I am not too familiar with the situation.

    It only looks like IE and firefox timeout. Where do you see the 301 error?

    Also, the call to connect in your code times out. I set urlConnection.setInstanceFollowRedirects(false) and it still times out. Shouldn't it not try to redirect at that point?

  5. #5
    Steel_Bird is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Blank “Location” header in HttpURLCONNECTION but redirect happens in IE

    Quote Originally Posted by shall View Post
    I have to admit I am not too familiar with the situation.

    It only looks like IE and firefox timeout. Where do you see the 301 error?

    Also, the call to connect in your code times out. I set urlConnection.setInstanceFollowRedirects(false) and it still times out. Shouldn't it not try to redirect at that point?
    The 301 redirect part is just an assumption on my side, because if you notice in IE and FF www0.bank.... redirects to www7.bank.... . But I guess the problem lies not with the redirects but the URL itself. It does not seem to be executing the try block itself and goes to the Catch block. I caught the exception and this i what I got

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target

    Could this be the problem for the URL not executing?

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

Similar Threads

  1. C# HttpURLConnection to java
    By jmorr212 in forum Networking
    Replies: 0
    Last Post: 02-24-2011, 10:29 AM
  2. How to redirect to a specified location from a jsp
    By sasi.tati in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-03-2010, 08:54 AM
  3. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 03:53 PM
  4. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM
  5. Replies: 21
    Last Post: 05-21-2009, 01:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •