URLConnection problem

Discussion in 'Java' started by Marcelo, Nov 30, 2005.

  1. Marcelo

    Marcelo Guest

    Dear Java Programmers,

    I am just getting crazy with this stuff.
    I am asking for the length of a URLConnection, but sometimes the given URLOject has no information or the address doesn't exists.

    URL url = new URL("http://www.alientech.to/pic/exelixis03/Auto-Exelixis_045.JPG");
    URLConnection connection = url.openConnection();
    //Then we ask for the length
    int length = connection.getContentLength();


    However, if the host is unknown the application stops in the last line without giving a time out. I have tried with a connection.setConnectTimeout(5000); but I still have the same problem.

    How Can I test the validity of a URL (something like a ping) without being blocked?
    If I am blocked with a URLConnection, how can interrumpt the channel ? Or even better, is it possible to close the "port" of this URLConnection ?

    thanks a lot,

    Marcelo

    PS: I have already tried to make connection=null, but that doesn't work (the channel is still active).
     
    Marcelo, Nov 30, 2005
    #1
    1. Advertising

  2. Marcelo

    Chris Smith Guest

    Marcelo <> wrote:
    > How Can I test the validity of a URL (something like a ping) without
    > being blocked?


    It appears that setConnectTimeout works only for the connection to the
    remote host, not for DNS lookups before the connection is made. You can
    pre-attempt the DNS lookup using JNDI, and set the JNDI environment
    property com.sun.jndi.dns.timeout.initial to a value in milliseconds.
    Then use the normal URLConnection mechanism to set the timeout for the
    communication with the remote host itself.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Nov 30, 2005
    #2
    1. Advertising

  3. Marcelo

    Marcelo Guest

    Hi, I have found another way to do it


    HttpURLConnection.setFollowRedirects(false);

    connection = (HttpURLConnection) urlObject.openConnection();

    //Request Properties
    connection.setRequestMethod("HEAD");
    connection.setRequestProperty("User-Agent",
    "Mozilla/4.0 (compatible;MSIE 5.5; Windows NT 5.0;H010818)" );

    //Some other parameters that may help...
    connection.setAllowUserInteraction( false );
    connection.setDoInput( true );
    connection.setDoOutput( false );
    connection.setUseCaches( false );

    boolean pageExists = (connection.getResponseCode() == HttpURLConnection.HTTP_OK);


    thanks for your help,

    Marcelo
     
    Marcelo, Nov 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristoffel

    URLConnection and proxy

    Kristoffel, Jul 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,584
    Kristoffel
    Jul 27, 2003
  2. Testbm
    Replies:
    1
    Views:
    854
    John Davison
    Oct 11, 2004
  3. Replies:
    3
    Views:
    785
  4. Replies:
    2
    Views:
    3,644
  5. vj
    Replies:
    2
    Views:
    2,240
    Real Gagnon
    Nov 16, 2006
Loading...

Share This Page