public class TimedURLConnection
extends java.lang.Object
URLConnection
wrapper that allows a connection timeout to be set, support for gzip
streaming, GET and POST data. A timout is useful for applications that need to retrieve content from a URL
without hanging if the remote server does not respond within a given period of time. Throws a URLConnectionTimedOutException
if the connection is not made within the allotted time or a IOException
if the connection fails for some other reason, such as an HTTP type 500 or 403 error.
The static methods importURL(java.lang.String, int)
and getInputStream(java.lang.String, int)
are provided for convenience.
Example that uses the static getInputStream method:
try {
// Get an input stream for the remote content (throws exeception if timeout occurs):
// Process the InputStream as desired. In this example, the InputStream is used to create a dom4j XML DOM:
// Now the DOM is ready for use...
import org.dlese.dpc.util.TimedURLConnection;
import org.dlese.dpc.util.URLConnectionTimedOutException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
// Plus other imports...
} catch (URLConnectionTimedOutException exc) {
InputStream istm = TimedURLConnection.getInputStream("http://example.org/remoteData.xml", 2000);
...
try{
SAXReader reader = new SAXReader();
Document document = reader.read(istm);
} catch ( DocumentException e ) {
// Handle the Exception as desired...
}
...
// The URLConnection timed out...
} catch (IOException ioe) {
// The URLConnection threw an IOException while attempting to connect...
}
URLConnection
Constructor and Description |
---|
TimedURLConnection() |
Modifier and Type | Method and Description |
---|---|
static java.io.InputStream |
getInputStream(java.lang.String url,
int timeOutPeriod)
Gets an InputStream for the given URL, timing out if the remote server does not respond within the given
number of milliseconds.
|
static java.io.InputStream |
getInputStream(java.net.URL url,
int timeOutPeriod)
Gets an InputStream for the given URL, timing out if the remote server does not respond within the given
number of milliseconds.
|
static java.io.InputStream |
getInputStream(java.net.URL url,
java.lang.String postData,
int timeOutPeriod)
Gets an InputStream for the given URL, timing out if the remote server does not respond within the given
number of milliseconds.
|
static long |
getUrlLastModifiedTime(java.net.URL url,
int timeOutPeriod)
Gets the last modified time of the URL, timing out if the remote server does not respond within the given
number of milliseconds.
|
static java.lang.String |
importURL(java.lang.String url,
int timeOutPeriod)
Imports the content of a given URL into a String using the default character encoding, timing out if the
remote server does not respond within the given number of milliseconds.
|
static java.lang.String |
importURL(java.lang.String url,
java.lang.String characterEncoding,
int timeOutPeriod)
Imports the content of a given URL into a String using the given character encoding timing out if the
remote server does not respond within the given number of milliseconds.
|
static java.lang.String |
importURL(java.lang.String url,
java.lang.String postData,
java.lang.String characterEncoding,
int timeOutPeriod)
Imports the content of a given URL into a String using the given character encoding, using POST data if
indicated or GET, timing out if the remote server does not respond within the given number of
milliseconds.
|
public static java.lang.String importURL(java.lang.String url, int timeOutPeriod) throws java.io.IOException, URLConnectionTimedOutException
url
- The URL to importtimeOutPeriod
- Milliseconds to wait before timing outjava.io.IOException
- If IO ErrorURLConnectionTimedOutException
- If timeout occurs before the server respondspublic static java.lang.String importURL(java.lang.String url, java.lang.String characterEncoding, int timeOutPeriod) throws java.io.IOException, URLConnectionTimedOutException
url
- The URL to importtimeOutPeriod
- Milliseconds to wait before timing outcharacterEncoding
- The character encoding to use, for example 'UTF-8'java.io.IOException
- If IO ErrorURLConnectionTimedOutException
- If timeout occurs before the server respondspublic static java.lang.String importURL(java.lang.String url, java.lang.String postData, java.lang.String characterEncoding, int timeOutPeriod) throws java.io.IOException, URLConnectionTimedOutException
url
- The URL to importtimeOutPeriod
- Milliseconds to wait before timing outcharacterEncoding
- The character encoding to use, for example 'UTF-8'postData
- Data to POST in the request of the form
parm1=value1¶m2=value2 or null to use GET (pass all params in the url)java.io.IOException
- If IO ErrorURLConnectionTimedOutException
- If timeout occurs before the server respondspublic static java.io.InputStream getInputStream(java.lang.String url, int timeOutPeriod) throws java.io.IOException, URLConnectionTimedOutException
url
- The URL to importtimeOutPeriod
- Milliseconds to wait before timing outjava.io.IOException
- If IO ErrorURLConnectionTimedOutException
- If timeout occurs before the server respondspublic static java.io.InputStream getInputStream(java.net.URL url, int timeOutPeriod) throws java.io.IOException, URLConnectionTimedOutException
url
- The URL to importtimeOutPeriod
- Milliseconds to wait before timing outjava.io.IOException
- If IO ErrorURLConnectionTimedOutException
- If timeout occurs before the server respondspublic static java.io.InputStream getInputStream(java.net.URL url, java.lang.String postData, int timeOutPeriod) throws java.io.IOException, URLConnectionTimedOutException
url
- The URL to importtimeOutPeriod
- Milliseconds to wait before timing outpostData
- Data to POST in the request of the form
parm1=value1¶m2=value2 or null to use GET (pass all params in the url)java.io.IOException
- If IO ErrorURLConnectionTimedOutException
- If timeout occurs before the server respondspublic static long getUrlLastModifiedTime(java.net.URL url, int timeOutPeriod) throws java.io.IOException, URLConnectionTimedOutException
url
- The URL to checktimeOutPeriod
- Milliseconds to wait before timing outjava.io.IOException
- If IO ErrorURLConnectionTimedOutException
- If timeout occurs before the server responds