mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-18 08:19:51 +00:00
retry when socket times out
git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@79 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
parent
edd72a64c4
commit
a3be919104
1 changed files with 40 additions and 24 deletions
|
@ -23,6 +23,7 @@ import java.io.OutputStreamWriter;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
@ -50,6 +51,12 @@ public final class ParserUtils
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CharSequence scrape(final String url, final String request) throws IOException
|
public static CharSequence scrape(final String url, final String request) throws IOException
|
||||||
|
{
|
||||||
|
int tries = 3;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
final StringBuilder buffer = new StringBuilder(SCRAPE_INITIAL_CAPACITY);
|
final StringBuilder buffer = new StringBuilder(SCRAPE_INITIAL_CAPACITY);
|
||||||
final URLConnection connection = new URL(url).openConnection();
|
final URLConnection connection = new URL(url).openConnection();
|
||||||
|
@ -80,6 +87,15 @@ public final class ParserUtils
|
||||||
pageReader.close();
|
pageReader.close();
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
catch (final SocketTimeoutException x)
|
||||||
|
{
|
||||||
|
if (tries-- > 0)
|
||||||
|
System.out.println("socket timed out, retrying...");
|
||||||
|
else
|
||||||
|
throw x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static final Pattern P_ENTITY = Pattern.compile("&(?:#(x[\\da-f]+|\\d+)|(amp|quot|apos));");
|
private static final Pattern P_ENTITY = Pattern.compile("&(?:#(x[\\da-f]+|\\d+)|(amp|quot|apos));");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue