Home > Archive > Java Help > January 2007 > HttpURLConnection locks up on connect()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
HttpURLConnection locks up on connect()
|
|
| phillip.s.powell@gmail.com 2007-01-23, 7:30 pm |
| public static boolean isReachableURL(URL url)
throws MalformedURLException, IOException, Exception {
System.out.println("Testing to see if URL connects");
HttpURLConnection conn =
(HttpURLConnection)url.openConnection();
System.out.println("Created HttpURLConnection object");
conn.connect();
System.out.println("connecting..");
boolean isConnected = (conn.getContentLength() > 0);
System.out.println("disconnecting..");
conn.disconnect();
System.out.println("disconnected");
return isConnected;
}
I am trying to simply determine if a URL provided is a "reachable URL",
if so, return true, else, return false.
On most URLs I have no problem distinguishing reachable from
unreachable, but if you try something like this:
http://blah.com
It will lock up tight as a drum and I have to break into Windows Task
Manager to stop the process as it runs as an infinite loop at this
point.
So how can I reliably determine if a URL is reachable or unreachable?
Thanx
Phil
| |
| Knute Johnson 2007-01-23, 7:30 pm |
| phillip.s.powell@gmail.com wrote:
> public static boolean isReachableURL(URL url)
> throws MalformedURLException, IOException, Exception {
> System.out.println("Testing to see if URL connects");
> HttpURLConnection conn =
> (HttpURLConnection)url.openConnection();
> System.out.println("Created HttpURLConnection object");
> conn.connect();
> System.out.println("connecting..");
> boolean isConnected = (conn.getContentLength() > 0);
> System.out.println("disconnecting..");
> conn.disconnect();
> System.out.println("disconnected");
> return isConnected;
> }
>
> I am trying to simply determine if a URL provided is a "reachable URL",
> if so, return true, else, return false.
>
> On most URLs I have no problem distinguishing reachable from
> unreachable, but if you try something like this:
>
> http://blah.com
>
> It will lock up tight as a drum and I have to break into Windows Task
> Manager to stop the process as it runs as an infinite loop at this
> point.
>
> So how can I reliably determine if a URL is reachable or unreachable?
>
> Thanx
> Phil
>
I don't know what the default timeout is on an HTTPURLConnection but it
will eventually time out. blah.com however does have a web server but
it is the slowest I have ever seen. It could be that you just need a
little patience.
--
Knute Johnson
email s/nospam/knute/
| |
| phillip.s.powell@gmail.com 2007-01-23, 7:30 pm |
|
Knute Johnson wrote:
> phillip.s.powell@gmail.com wrote:
>
> I don't know what the default timeout is on an HTTPURLConnection but it
> will eventually time out. blah.com however does have a web server but
> it is the slowest I have ever seen. It could be that you just need a
> little patience.
>
I have no way of changing the default timeout in HttpURLConnection as
it has no property nor method that exists within the J2SE 1.4.2 API
that tells me how to set it. I was sitting there for a minute w/o any
results.
> --
>
> Knute Johnson
> email s/nospam/knute/
| |
| phillip.s.powell@gmail.com 2007-01-23, 7:30 pm |
|
phillip.s.powell@gmail.com wrote:
> Knute Johnson wrote:
>
> I have no way of changing the default timeout in HttpURLConnection as
> it has no property nor method that exists within the J2SE 1.4.2 API
> that tells me how to set it. I was sitting there for a minute w/o any
> results.
Got it! And it was so easy! No wonder nobody wanted to answer this one
/**
* Determine if the given {@link java.net.URL} is reachable
* @param url {@link java.net.URL}
* @return {@link java.lang.Boolean}
* @throws java.net.MalformedURLException To be thrown if the
{@link java.net.URL} is malformed
* @throws java.io.IOException To be thrown if an I/O error occurs
* @throws java.lang.Exception To be thrown if all other errors
occur
*/
public static boolean isReachableURL(URL url)
throws MalformedURLException, IOException, Exception {
HttpURLConnection conn =
(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(10000); // SET CONNECTION TIMEOUT TO 10
SECS
conn.setReadTimeout(10000); // SET I/O READ TIMEOUT TO 10
SECS
// NO NEED TO USE connect() METHOD as getContentLength()
IMPLICITLY CONNECTS
boolean isConnected = (conn.getContentLength() > 0);
conn.disconnect();
return isConnected;
}
Phil
[color=darkred]
>
| |
|
| phillip.s.powell@gmail.com wrote:
> Got it! And it was so easy! No wonder nobody wanted to answer this one
> public static boolean isReachableURL(URL url)
You might consider renaming the method "wasReachable()" or
"isProbablyReachable()", since reachability can change right after you check it.
- Lew
| |
| phillip.s.powell@gmail.com 2007-01-24, 4:25 am |
|
On Jan 23, 6:25 pm, Lew <l...@nowhere.com> wrote:
> phillip.s.pow...@gmail.com wrote:
> "isProbablyReachable()", since reachability can change right after you check it.
>
You make it sound futile to attempt to check a remote URL
> - Lew
|
|
|
|
|