List of usage examples for org.apache.http.client HttpClient execute
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;
From source file:org.androidappdev.codebits.CodebitsApi.java
/** * Perform an HTTP request/*from w w w . jav a2s.c o m*/ * * @param url * the URL for the request * @return a String with the result of the request */ private static String performHttpRequest(String url) { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url); try { HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line = null; while ((line = reader.readLine()) != null) { builder.append(line); } } catch (Exception e) { Log.d(TAG, "Exception", e); } return builder.toString(); }
From source file:com.hat.tools.HttpUtils.java
public static void SendRequest(String url, Handler handler) { HttpGet request = new HttpGet(url); HttpClient httpClient = new DefaultHttpClient(); byte[] data = null; InputStream in = null;// w w w. j a v a 2 s .c o m try { HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); in = response.getEntity().getContent(); byte[] buf = new byte[1024]; int len = 0; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } data = out.toByteArray(); Message msg = new Message(); msg.what = HTTP_FINISH; Bundle bundle = new Bundle(); bundle.putByteArray("data", data); msg.setData(bundle); handler.sendMessage(msg); out.close(); } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.jboss.demo.loanmanagement.client.RestCommandRunner.java
/** * @param restCommand the command to execute (cannot be <code>null</code>) * @return the response as a JSON object (never <code>null</code>) * @throws Exception if there are problems executing the command *//*from w ww .j a v a2 s. co m*/ public static JSONObject run(final Command restCommand) throws Exception { final HttpGet httpGet = new HttpGet(restCommand.getUrl()); { // headers if (restCommand.useAuthorization()) { httpGet.addHeader("Authorization", restCommand.geAuthorizationHeader()); //$NON-NLS-1$ } httpGet.addHeader("Accept", "*/*;charset=utf-8"); //$NON-NLS-1$ //$NON-NLS-2$ } final HttpClient httpClient = new DefaultHttpClient(); InputStream is = null; try { final HttpResponse httpResponse = httpClient.execute(httpGet); final HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); //$NON-NLS-1$ final StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append('\n'); } final String content = sb.toString(); final JSONObject result = new JSONObject(content); return result; } finally { if (is != null) { try { is.close(); } catch (final IOException e) { // nothing to do } } } }
From source file:com.chiorichan.maven.MavenUtils.java
public static boolean downloadFile(String url, File dest) throws ClientProtocolException, IOException { HttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() != 200) { PluginManager.getLogger()/* w w w. j a v a 2s .c om*/ .severe("Could not download the file `" + url + "`, webserver returned `" + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine().getReasonPhrase() + "`"); return false; } InputStream instream = entity.getContent(); FileUtils.copyInputStreamToFile(instream, dest); return true; }
From source file:com.mothsoft.alexis.util.NetworkingUtil.java
public static HttpClientResponse get(final URL url, final String etag, final Date lastModifiedDate) throws IOException { final HttpGet get = new HttpGet(url.toExternalForm()); get.addHeader("Accept-Charset", "UTF-8"); if (etag != null) { get.addHeader("If-None-Match", etag); }/*w w w .ja v a2 s. c o m*/ if (lastModifiedDate != null) { get.addHeader("If-Modified-Since", DateUtils.formatDate(lastModifiedDate)); } int statusCode = -1; String responseEtag = null; Date responseLastModifiedDate = null; final HttpClient client = getClient(); HttpResponse response = client.execute(get); statusCode = response.getStatusLine().getStatusCode(); InputStream is = null; Charset charset = null; if (statusCode == 304) { responseEtag = etag; responseLastModifiedDate = lastModifiedDate; } else { final Header responseEtagHeader = response.getFirstHeader("Etag"); if (responseEtagHeader != null) { responseEtag = responseEtagHeader.getValue(); } final Header lastModifiedDateHeader = response.getFirstHeader("Last-Modified"); if (lastModifiedDateHeader != null) { try { responseLastModifiedDate = DateUtils.parseDate(lastModifiedDateHeader.getValue()); } catch (DateParseException e) { responseLastModifiedDate = null; } } final HttpEntity entity = response.getEntity(); // here's where to do intelligent checking of content type, content // length, etc. if (entity.getContentLength() > MAX_CONTENT_LENGTH) { get.abort(); throw new IOException("Exceeded maximum content length, length is: " + entity.getContentLength()); } is = entity.getContent(); charset = getCharset(entity); } return new HttpClientResponse(get, statusCode, responseEtag, responseLastModifiedDate, is, charset); }
From source file:Main.java
/** * Make POST request and return plain response * @param url/*www.j a v a 2 s . co m*/ * @param pairs * @return Plain text response * @throws Exception */ public static String makeHttpPostRequest(String url, List<NameValuePair> pairs) throws Exception { BufferedReader in = null; String result = ""; try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = client.execute(post); 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 + NL); } in.close(); result = sb.toString(); // System.out.println(result); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
From source file:eu.skillpro.ams.pscm.connector.amsservice.ui.RetrieveSEEHandler.java
/** * Returns a list of SEEs, retrieved by the server's retrieveSEEs method. * @param newOnly <code>true</code> to get only the newly registered SEEs, <code>false</code> to get all SEEs * @return a list of SEE transfer objects * @throws IOException if there were any problems with the sonnection to the server *//* ww w. ja va2s .com*/ public static List<SEETO> getSEETOs(boolean newOnly) throws IOException { String serviceName = "retrieveSEEs"; String parameters = "?newOnly=" + newOnly; HttpGet request = new HttpGet(AMSServiceUtility.serviceAddress + serviceName + parameters); request.setHeader("Content-type", "application/json"); HttpClient client = HttpClientBuilder.create().build(); ; HttpResponse response = client.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } if (sb.length() == 0) { return new ArrayList<SEETO>(); } else { List<SEETO> result = JSONUtility.convertToList(sb.toString(), new TypeToken<List<SEETO>>() { }.getType()); System.out.println("Number of retrieved SEEs: " + result.size()); return result; } }
From source file:Main.java
public static String getRepsonseString(String url, Map<String, String> params) { HttpPost request = new HttpPost(url); List<NameValuePair> p = new ArrayList<NameValuePair>(); for (String key : params.keySet()) { p.add(new BasicNameValuePair(key, params.get(key))); }/* w ww . j ava 2s . c o m*/ HttpClient httpClient = new DefaultHttpClient(); String result = null; try { request.setEntity(new UrlEncodedFormEntity(p, HTTP.UTF_8)); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == 200) { result = new String(EntityUtils.toString(response.getEntity()).getBytes("ISO_8859_1"), "UTF-8"); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (httpClient != null) httpClient.getConnectionManager().shutdown(); } return result; }
From source file:Main.java
public static String getJSONFromUrl(String url) { try {//from ww w . j a v a 2 s .co m HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); // httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); String tempResponse = EntityUtils.toString(httpResponse.getEntity()); return tempResponse; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; }
From source file:com.deployd.Deployd.java
public static JSONObject delete(String uri) throws ClientProtocolException, IOException, JSONException { HttpDelete post = new HttpDelete(endpoint + uri); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); ByteArrayEntity e = (ByteArrayEntity) response.getEntity(); InputStream is = e.getContent(); String data = new Scanner(is).next(); JSONObject result = new JSONObject(data); return result; }