public static boolean canGetWebResources(ProxyConfiguration proxyConfiguration, int timeout) { try { // TODO: add better method to check web resources int result = testHTTPConnection(new URI("http://www.un.org/"), proxyConfiguration, timeout); // int rawresult = testHTTPConnection(new URI("http://157.150.34.32"), // proxyConfiguration, timeout); switch (result) { case HttpURLConnection.HTTP_OK: case HttpURLConnection.HTTP_CREATED: case HttpURLConnection.HTTP_NO_CONTENT: case HttpURLConnection.HTTP_NOT_AUTHORITATIVE: case HttpURLConnection.HTTP_ACCEPTED: case HttpURLConnection.HTTP_PARTIAL: case HttpURLConnection.HTTP_RESET: return true; default: return false; } } catch (URISyntaxException e) { LogWrapper.w(TAG, e.toString()); // APL.getEventReport().send(e); } return false; }
private void log(String tag, String msg, int logLevel) { switch (logLevel) { case Log.DEBUG: d(tag, msg); break; case Log.ERROR: e(tag, msg); break; case Log.INFO: i(tag, msg); break; case Log.ASSERT: a(tag, msg); break; case Log.WARN: w(tag, msg); break; default: v(tag, msg); break; } }
public static int testHTTPConnection( URI uri, ProxyConfiguration proxyConfiguration, int timeout) { int step = 0; while (step < 5) { try { URL url = uri.toURL(); if (proxyConfiguration != null && proxyConfiguration.getProxyType() == Type.HTTP) { System.setProperty("http.proxyHost", proxyConfiguration.getProxyIPHost()); System.setProperty("http.proxyPort", proxyConfiguration.getProxyPort().toString()); } else { System.setProperty("http.proxyHost", ""); System.setProperty("http.proxyPort", ""); } HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setReadTimeout(timeout); httpURLConnection.setConnectTimeout(timeout); int result = httpURLConnection.getResponseCode(); return result; } catch (Exception e) { LogWrapper.w(TAG, e.toString()); } step++; try { Thread.sleep(500); } catch (InterruptedException e) { APL.getEventReport().send(e); return -1; } } return -1; }