List of usage examples for org.apache.http.impl.client DefaultHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:org.apiwatch.util.IO.java
public static APIScope getAPIData(String source, String format, String encoding, String username, String password) throws IOException, SerializationError, HttpException { File file = new File(source); APIScope scope = null;//from w w w . j a va 2 s . co m if (file.isFile()) { if (format == null) { /* get format from file extension */ format = source.substring(source.lastIndexOf('.') + 1); } InputStream in = new FileInputStream(file); Reader reader = new InputStreamReader(in, encoding); scope = Serializers.loadAPIScope(reader, format); reader.close(); in.close(); } else { /* maybe source is a URL */ DefaultHttpClient client = new DefaultHttpClient(); if (username != null && password != null) { client.getCredentialsProvider().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(username, password)); } HttpResponse response = client.execute(new HttpGet(source)); if (response.getStatusLine().getStatusCode() >= 400) { throw new HttpException(response.getStatusLine().getReasonPhrase()); } HttpEntity entity = response.getEntity(); ContentType contentType = ContentType.fromHeader(entity.getContentType().getValue()); if (entity.getContentEncoding() != null) { encoding = entity.getContentEncoding().getValue(); } else if (contentType.charset != null) { encoding = contentType.charset; } if (format == null) { format = contentType.type; } InputStream in = entity.getContent(); Reader reader = new InputStreamReader(in, encoding); scope = Serializers.loadAPIScope(reader, format); reader.close(); in.close(); client.getConnectionManager().shutdown(); } return scope; }
From source file:org.neo4j.nlp.examples.author.main.java
private static String executePost(String targetURL, String payload) { try {//w w w . jav a2s.c om DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(targetURL); StringEntity input = new StringEntity(payload); input.setContentType("application/json"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); StringBuilder output = new StringBuilder(); while (br.read() != -1) { output.append(br.readLine()).append('\n'); } httpClient.getConnectionManager().shutdown(); return output.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:no.norrs.projects.andronary.utils.HttpUtil.java
public static HttpResponse GET(URL url, UsernamePasswordCredentials creds) throws IOException, URISyntaxException { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), creds); HttpGet httpGet = new HttpGet(url.toURI()); httpGet.addHeader("Accept", "application/json"); httpGet.addHeader("User-Agent", "Andronary/0.1"); HttpResponse response;//from w ww. j a va 2 s.com return httpClient.execute(httpGet); }
From source file:middleware.HTTPRequest.java
public static void doPostVuelo(String jsonRequest) throws UnsupportedEncodingException, IOException { String url = "http://localhost:8084/MVIv2/webapi/vuelos"; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(url); StringEntity input = new StringEntity(jsonRequest); input.setContentType("application/json"); postRequest.setEntity(input);/* w w w. ja v a2 s.co m*/ HttpResponse response = httpClient.execute(postRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("ERROR AL INSERTAR DEL TIPO: " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } httpClient.getConnectionManager().shutdown(); }
From source file:middleware.HTTPRequest.java
public static void doPostReserva(String jsonRequest) throws UnsupportedEncodingException, IOException { String url = "http://localhost:8084/MVIv2/webapi/reservas"; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(url); StringEntity input = new StringEntity(jsonRequest); input.setContentType("application/json"); postRequest.setEntity(input);// w ww. j a v a 2 s .c om HttpResponse response = httpClient.execute(postRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("ERROR AL INSERTAR DEL TIPO: " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } httpClient.getConnectionManager().shutdown(); }
From source file:org.jboss.jdf.stacks.StacksFactory.java
public static Stacks create(final StacksConfiguration stacksConfig) throws IOException { InputStream inputStream = null; HttpResponse response = null;/*from w w w.jav a 2 s .com*/ DefaultHttpClient client = null; final URL url = stacksConfig.getUrl(); try { if (url.getProtocol().startsWith("file")) { inputStream = new FileInputStream(new File(url.toURI())); } else { client = new DefaultHttpClient(); configureProxy(client, stacksConfig); final HttpGet method = new HttpGet(url.toURI()); response = client.execute(method); final HttpEntity entity = response.getEntity(); if (entity != null) { inputStream = entity.getContent(); } } return new Parser().parse(inputStream); } catch (URISyntaxException e) { // TODO cleanup throw new IllegalStateException(e); } finally { HttpClientUtils.closeQuietly(response); HttpClientUtils.closeQuietly(client); Parser.safeClose(inputStream); } }
From source file:att.jaxrs.client.Library.java
public static String addLibrary(Library library) { List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); urlParameters.add(new BasicNameValuePair("category_id", Integer.toString(library.getCategory_id()))); urlParameters.add(new BasicNameValuePair("title", library.getTitle())); urlParameters.add(new BasicNameValuePair("published_date", library.getPublished_date())); urlParameters.add(new BasicNameValuePair("url", library.getUrl())); HttpResponse result;/*from w w w . j a va 2 s .c o m*/ String resultStr = ""; try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(Constants.INSERT_LIBRARY_RESOURCE); post.setEntity(new UrlEncodedFormEntity(urlParameters)); result = httpClient.execute(post); System.out.println(Constants.RESPONSE_STATUS_CODE + result); resultStr = Util.getStringFromInputStream(result.getEntity().getContent()); System.out.println(Constants.RESPONSE_BODY + resultStr); } catch (Exception e) { System.out.println(e.getMessage()); } return resultStr; }
From source file:net.okjsp.imageloader.ImageFetcher.java
/** * Download a bitmap from a URL, write it to a disk and return the File pointer. This * implementation uses a simple disk cache. * * @param context The context to use//from w w w.ja v a 2s . c om * @param urlString The URL to fetch * @return A File pointing to the fetched bitmap */ public static File downloadBitmap(Context context, String urlString) { final File cacheDir = DiskLruCache.getDiskCacheDir(context, HTTP_CACHE_DIR); final DiskLruCache cache = DiskLruCache.openCache(context, cacheDir, HTTP_CACHE_SIZE); final File cacheFile = new File(cache.createFilePath(urlString)); if (cache.containsKey(urlString)) { if (BuildConfig.DEBUG && DEBUG_LOG) { Log.d(TAG, "downloadBitmap - found in http cache - " + urlString); } return cacheFile; } if (BuildConfig.DEBUG && DEBUG_LOG) { Log.d(TAG, "downloadBitmap - downloading - " + urlString); } ImageLoaderUtils.disableConnectionReuseIfNecessary(); BufferedOutputStream out = null; InputStream in = null; final DefaultHttpClient client = new DefaultHttpClient(); final HttpGet getRequest = new HttpGet(urlString); try { if (DEBUG_LOG) Log.d(TAG, "getRequest:" + getRequest.toString()); HttpResponse response = client.execute(getRequest); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return null; } final HttpEntity entity = response.getEntity(); if (entity != null) { in = new BufferedInputStream(entity.getContent(), ImageLoaderUtils.IO_BUFFER_SIZE); } out = new BufferedOutputStream(new FileOutputStream(cacheFile), ImageLoaderUtils.IO_BUFFER_SIZE); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); in = null; return cacheFile; } catch (final IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } in = null; } if (out != null) { try { out.close(); } catch (final IOException e) { e.printStackTrace(); } } } return null; }
From source file:org.neo4j.nlp.examples.sentiment.main.java
private static String executePost(String targetURL, String payload) { try {//from w w w.j a v a2s . c om DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(targetURL); StringEntity input = new StringEntity(payload); input.setContentType("application/json"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); StringBuilder output = new StringBuilder(); while (br.read() != -1) { output.append(br.readLine()).append('\n'); } httpClient.getConnectionManager().shutdown(); return "{" + output.toString(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:no.norrs.projects.andronary.service.utils.HttpUtil.java
public static HttpResponse GET(URL url, UsernamePasswordCredentials creds) throws IOException, URISyntaxException { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), creds); HttpGet httpGet = new HttpGet(url.toURI()); httpGet.addHeader("Accept", "application/json"); httpGet.addHeader("User-Agent", "Andronary/0.1"); HttpResponse response;//w w w. ja v a2 s. c o m return httpClient.execute(httpGet); }