/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package buzzerproxy; import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; /** * * @author Enger */ public class CheckStatus { private String contentLenght = ""; public boolean isAlive(String urlToGet) throws ClientProtocolException, IOException { boolean isAlive = false; String urlGetMe = null; setContentLenght("-1"); try { URL url = new URL(urlToGet); URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null); // System.out.println("URI " + uri.toString() + " is OK"); urlGetMe = uri.toString(); } catch (MalformedURLException e) { System.out.println("URL " + urlToGet + " is a malformed URL"); } catch (URISyntaxException e) { System.out.println("URI " + urlToGet + " is a malformed URL"); } HttpClient client = new DefaultHttpClient(); HttpGet method = new HttpGet(urlGetMe); HttpResponse httpResponse = null; try { httpResponse = client.execute(method); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { isAlive = true; } } catch (Exception e) { } if (httpResponse != null) { Header h[] = httpResponse.getAllHeaders(); for (int i = 0; i < h.length; i++) { // System.out.println( h[i].getName() + " => " + h[i].getValue()) ; if (h[i].getName().contains("Content-Length")) { setContentLenght(h[i].getValue()); break; } // if (h[i].getName().contains("Content-Length")) { } // for (int i = 0; i < h.length; i++) { } // if ( httpResponse != null ) { return isAlive; } // public boolean isAlive(String url){ /** * @return the contentLenght */ public String getContentLenght() { return contentLenght; } /** * @param contentLenght the contentLenght to set */ public void setContentLenght(String contentLenght) { this.contentLenght = contentLenght; } }