List of usage examples for org.apache.http.client HttpClient execute
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;
From source file:com.devdungeon.httpexamples.HttpGetExample.java
private static String get(String url) { HttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 1000 * 5); HttpConnectionParams.setSoTimeout(params, 1000 * 5); HttpGet request = new HttpGet(url); HttpResponse response = null;//from www . ja v a2 s . c o m try { response = client.execute(request); } catch (IOException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } BufferedReader rd; String html = ""; String line = null; try { rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); while ((line = rd.readLine()) != null) { html += line + "\n"; } } catch (IOException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedOperationException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } return html; }
From source file:Main.java
public static String downloadPage(String url) { Log.v(DEBUG_TAG, url);// www . j a v a 2 s .co m String page = ""; BufferedReader in = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); HttpParams params = request.getParams(); HttpConnectionParams.setSoTimeout(params, 60000); // 1 minute timeout HttpResponse response = client.execute(request); //Predifined buffer size /* * in = new BufferedReader( new InputStreamReader( response.getEntity().getContent()),8*2000); * * */ in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; //String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line); } in.close(); page = sb.toString(); Log.v(DEBUG_TAG, "Pagina descargada --> " + page); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { Log.v(DEBUG_TAG, "Error en la descarga de la pagina"); e.printStackTrace(); } } } return page; }
From source file:com.foobnix.api.vkontakte.VkOld.java
public static String[] getVKUserPass(String url) { HttpClient client = new DefaultHttpClient(); HttpPost http = new HttpPost(url); HttpResponse response;//from w ww. j a v a 2s . c o m String jString = ":"; try { response = client.execute(http); HttpEntity entity = response.getEntity(); jString = EntityUtils.toString(entity); LOG.d("result", jString, url); } catch (Exception e) { LOG.e("GEV Vk user pass error", e); } return jString.split(":"); }
From source file:realizer.com.schoolgenieparent.ServerUtilities.java
private static StringBuilder postUnregister(String EmpId, String did, String access) { String my = Config.URL + "deregisterStudentDevice/" + EmpId + "/" + did; Log.d("GCMDID", my); builder = new StringBuilder(); HttpGet httpGet = new HttpGet(my); HttpClient client = new DefaultHttpClient(); httpGet.setHeader("AccessToken", access); try {// w w w.j a v a 2s.c o m HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); Log.d("GCMSTATUS", "" + statusCode); if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } else { Log.e("Error", "Failed to Login"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { client.getConnectionManager().closeExpiredConnections(); client.getConnectionManager().shutdown(); } return builder; }
From source file:com.devdungeon.httpexamples.DownloadFile.java
private static void download(String url, String filepath) { HttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 1000 * 5); HttpConnectionParams.setSoTimeout(params, 1000 * 5); HttpGet request = new HttpGet(url); HttpResponse response = null;//from w w w.j a va2s. c o m try { response = client.execute(request); } catch (IOException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } BufferedReader rd; String line = null; try { rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); FileWriter fileWriter = new FileWriter(filepath); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); while ((line = rd.readLine()) != null) { bufferedWriter.write(line); bufferedWriter.newLine(); } bufferedWriter.close(); } catch (IOException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedOperationException ex) { Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.datacleaner.util.http.HttpXmlUtils.java
public static Element getRootNode(HttpClient httpClient, String url) throws InvalidHttpResponseException { logger.info("getRootNode({})", url); try {/* w w w . j a v a2 s . co m*/ HttpGet method = new HttpGet(url); HttpResponse response = httpClient.execute(method); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { logger.error("Response status code was: {} (url={})", statusCode, url); throw new InvalidHttpResponseException(url, response); } InputStream inputStream = response.getEntity().getContent(); Document document = createDocumentBuilder().parse(inputStream); return (Element) document.getFirstChild(); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new IllegalStateException("Could not get root XML node of url=" + url, e); } }
From source file:org.jboss.additional.testsuite.jdkall.present.clustering.cluster.ejb.xpc.StatefulWithXPCFailoverTestCase.java
private static String executeUrlWithAnswer(HttpClient client, URI url, String message) throws IOException { HttpResponse response = client.execute(new HttpGet(url)); try {//from w w w . j a va 2 s.c o m assertEquals(200, response.getStatusLine().getStatusCode()); Header header = response.getFirstHeader("answer"); Assert.assertNotNull(message, header); return header.getValue(); } finally { HttpClientUtils.closeQuietly(response); } }
From source file:org.xwiki.android.authenticator.rest.XWikiConnector.java
public static HttpResponse executeGet(String url, String user, String pass) throws IOException { HttpClient httpClient = getHttpClient(); HttpGet httpGet = createHtpGet(url, user, pass); return httpClient.execute(httpGet); }
From source file:Main.java
/** * Code adapted from http://stackoverflow.com/a/12854981 * @return Device external IP address or ERROR statement. *///from www. ja v a 2 s . c om public static String getExternalIP() { try { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON"); // HttpGet httpget = new HttpGet("http://whatismyip.com.au/"); // HttpGet httpget = new HttpGet("http://www.whatismyip.org/"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null && entity.getContentLength() > 0) { JSONObject json_data = new JSONObject(EntityUtils.toString(entity)); return json_data.getString("ip"); } else { Log.e(LOG_TAG, "Response error: " + response.getStatusLine().toString()); } } catch (Exception e) { Log.e(LOG_TAG, e.toString()); } return "ERROR"; }
From source file:org.wisdom.test.http.HttpClientHelper.java
/** * Executes the request.//from w w w .ja va 2 s. co m * * @param request the request * @param responseClass the response class * @param <T> the type of content expected in the response * @return the response * @throws Exception if something bad happens. */ public static <T> HttpResponse<T> request(HttpRequest request, Class<T> responseClass) throws Exception { HttpRequestBase requestObj = prepareRequest(request); // The DefaultHttpClient is thread-safe HttpClient client = ClientFactory.getHttpClient(); org.apache.http.HttpResponse response; try { response = client.execute(requestObj); HttpResponse<T> httpResponse = new HttpResponse<>(response, responseClass); requestObj.releaseConnection(); return httpResponse; } finally { requestObj.releaseConnection(); } }