1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| public static String getsource(String url)
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "ignoreCookies");
HttpGet httpget = new HttpGet(url);
StringBuilder source = new StringBuilder();
String resa = null;
try
{
HttpResponse response = httpclient.execute(httpget);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
long ms = System.currentTimeMillis();
boolean timeout_over = false;
String line;
while ((line = reader.readLine()) != null && !timeout_over)
{
source.append(line+"\r\n");
if(System.currentTimeMillis()-ms>10000)
{ timeout_over = true; }
}
if(timeout_over)
{
resa = "Erreur";
System.out.println("Timeout Over");
httpget.abort();
}
else
{ resa = source.toString().trim(); }
}
catch(IOException ioe)
{
resa = "Erreur";
System.out.println("IO exception");
}
catch(Exception e)
{
resa = "Erreur";
System.out.println("Exception");
}
return resa;
} |
Partager