List of usage examples for org.apache.http.client HttpClient execute
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;
From source file:org.vuphone.vandyupon.test.EventRequestTester.java
public static void main(String[] args) { HttpClient c = new DefaultHttpClient(); HttpPost post = new HttpPost("http://localhost:8080/vandyupon/events/"); post.addHeader("Content-Type", "application/x-www-form-urlencoded"); String params = "type=eventrequest&lat=36.1437&lon=-86.8046&dist=100&resp=xml&userid=chris"; post.setEntity(new ByteArrayEntity(params.toString().getBytes())); try {//from w w w . ja v a 2 s . co m HttpResponse resp = c.execute(post); resp.getEntity().writeTo(System.out); } catch (IOException e) { e.printStackTrace(); } }
From source file:test1.ApacheHttpRestClient2.java
public final static void main(String[] args) { HttpClient httpClient = new DefaultHttpClient(); try {// ww w . j a va 2s.co m // this ona api call returns results in a JSON format HttpGet httpGetRequest = new HttpGet("https://api.ona.io/api/v1/users"); // Execute HTTP request HttpResponse httpResponse = httpClient.execute(httpGetRequest); System.out.println("------------------HTTP RESPONSE----------------------"); System.out.println(httpResponse.getStatusLine()); System.out.println("------------------HTTP RESPONSE----------------------"); // Get hold of the response entity HttpEntity entity = httpResponse.getEntity(); // If the response does not enclose an entity, there is no need // to bother about connection release byte[] buffer = new byte[1024]; if (entity != null) { InputStream inputStream = entity.getContent(); try { int bytesRead = 0; BufferedInputStream bis = new BufferedInputStream(inputStream); while ((bytesRead = bis.read(buffer)) != -1) { String chunk = new String(buffer, 0, bytesRead); FileWriter file = new FileWriter("C:\\Users\\fred\\Desktop\\webicons\\output.txt"); //file.write(chunk.toJSONString()); file.write(chunk.toCharArray()); file.flush(); file.close(); System.out.print(chunk); System.out.println(chunk); } } catch (IOException ioException) { // In case of an IOException the connection will be released // back to the connection manager automatically ioException.printStackTrace(); } catch (RuntimeException runtimeException) { // In case of an unexpected exception you may want to abort // the HTTP request in order to shut down the underlying // connection immediately. httpGetRequest.abort(); runtimeException.printStackTrace(); } // try { // FileWriter file = new FileWriter("C:\\Users\\fred\\Desktop\\webicons\\output.json"); // file.write(bis.toJSONString()); // file.flush(); // file.close(); // // System.out.print(bis); // } catch (Exception e) { // } finally { // Closing the input stream will trigger connection release try { inputStream.close(); } catch (Exception ignore) { } } } } catch (ClientProtocolException e) { // thrown by httpClient.execute(httpGetRequest) e.printStackTrace(); } catch (IOException e) { // thrown by entity.getContent(); e.printStackTrace(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpClient.getConnectionManager().shutdown(); } }
From source file:postenergy.PostHttpClient.java
public static void main(String[] args) { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://iplant.dk/addData.php?n=mindass"); try {/*from ww w . ja v a 2 s. c o m*/ List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("?n", "=mindass")); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); if (line.startsWith("Input:")) { String key = line.substring(6); // do something with the key System.out.println("key:" + key); } } } catch (IOException e) { System.out.println("There was an error: " + e); } }
From source file:test.TestJavaService.java
/** * Main. The cmdline arguments either do or don't contain the flag -remote: that's the only supported flag. If it is specified, * we test code on the AWS instance. If it is not, we test code running locally. * The first non-flag argument is the "verb", which (currently) should be one of the verbs set at the top of source file * org.qcert.javasrc.Main. Remaining non-flag arguments are file names. There must be at least one. The number of such * arguments and what they should contain depends on the verb. * @throws Exception//from ww w. j a v a 2s . c o m */ public static void main(String[] args) throws Exception { /* Parse command line */ List<String> files = new ArrayList<>(); String loc = "localhost", verb = null; for (String arg : args) { if (arg.equals("-remote")) loc = REMOTE_LOC; else if (arg.startsWith("-")) illegal(); else if (verb == null) verb = arg; else files.add(arg); } /* Simple consistency checks and verb-specific parsing */ if (files.size() == 0) illegal(); String file = files.remove(0); String schema = null; switch (verb) { case "parseSQL": case "serialRule2CAMP": case "sqlSchema2JSON": if (files.size() != 0) illegal(); break; case "techRule2CAMP": if (files.size() != 1) illegal(); schema = files.get(0); break; case "csv2JSON": if (files.size() < 1) illegal(); break; default: illegal(); } /* Assemble information from arguments */ String url = String.format("http://%s:9879?verb=%s", loc, verb); byte[] contents = Files.readAllBytes(Paths.get(file)); String toSend; if ("serialRule2CAMP".equals(verb)) toSend = Base64.getEncoder().encodeToString(contents); else toSend = new String(contents); if ("techRule2CAMP".equals(verb)) toSend = makeSpecialJson(toSend, schema); else if ("csv2JSON".equals(verb)) toSend = makeSpecialJson(toSend, files); HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(url); StringEntity entity = new StringEntity(toSend); entity.setContentType("text/plain"); post.setEntity(entity); HttpResponse resp = client.execute(post); int code = resp.getStatusLine().getStatusCode(); if (code == HttpStatus.SC_OK) { HttpEntity answer = resp.getEntity(); InputStream s = answer.getContent(); BufferedReader rdr = new BufferedReader(new InputStreamReader(s)); String line = rdr.readLine(); while (line != null) { System.out.println(line); line = rdr.readLine(); } rdr.close(); s.close(); } else System.out.println(resp.getStatusLine()); }
From source file:com.everm.httpclient.HttpClientTest.java
/** * @param args/*w w w .j av a 2s .c o m*/ */ public static void main(String[] args) { String uri = "http://samsung-cps-000.s3.amazonaws.com/PDF/-137219737027956571"; HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(uri); HttpResponse response = null; long startTime = System.currentTimeMillis(); try { httpGet.addHeader("Date", "Mon, 18 Feb 2013 08:14:01 GMT"); httpGet.addHeader("Authorization", "AWS AKIAIULRL33KVNJCCNAQ:ac4P+U1n+ib/PkOCqyuH4lt9hK8="); // for (int i = 0; i < 100; i++) { response = client.execute(httpGet); // System.out.println(i + "___" + response.getStatusLine()); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); // } } catch (Exception e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); System.out.println("elapsed time :" + (endTime - startTime)); }
From source file:org.eclipse.lyo.adapter.tdb.clients.OSLCTriplestoreAdapterResourceCreationClient.java
public static void main(String[] args) { String baseHTTPURI = "http://localhost:" + OSLC4JTDBApplication.portNumber + "/oslc4jtdb"; String projectId = "default"; // URI of the HTTP request String tdbResourceCreationFactoryURI = baseHTTPURI + "/services/" + projectId + "/resources"; // create RDF to add to the triplestore Model resourceRDFModel = ModelFactory.createDefaultModel(); Resource resource = ResourceFactory .createResource("http://localhost:8585/oslc4jtdb/services/default/resources/newBlock4"); Property property = ResourceFactory/* w w w . java 2 s . c o m*/ .createProperty("http://localhost:8585/oslc4jtdb/services/default/resources/newProperty4"); RDFNode object = ResourceFactory .createResource("http://localhost:8585/oslc4jtdb/services/default/resources/newObject4"); resourceRDFModel.add(resource, property, object); StringWriter out = new StringWriter(); resourceRDFModel.write(out, "RDF/XML"); resourceRDFModel.write(System.out); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(tdbResourceCreationFactoryURI); String xml = out.toString(); HttpEntity entity; try { entity = new ByteArrayEntity(xml.getBytes("UTF-8")); post.setEntity(entity); post.setHeader("Accept", "application/rdf+xml"); HttpResponse response = client.execute(post); System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); } 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(); } }
From source file:com.test.restdev.simpleRestClient.java
public static void main(String[] args) throws ClientProtocolException, IOException { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://localhost:8080/restdev/resources/developers/rafi-kokos"); HttpGet request1 = new HttpGet("http://localhost:8081/restdev/resources/developers/rafi-kokos"); HttpGet request2 = new HttpGet("http://localhost:8082/restdev/resources/developers/rafi-kokos"); HttpResponse response;//from w ww. ja v a 2 s .c o m for (int i = 0; i < 10; i++) { if (i % 3 == 0) { response = client.execute(request); System.out.println("If (i%3==0) -> call no. " + i + " to uri:" + request.getURI()); } else if (i % 3 == 1) { response = client.execute(request1); System.out.println("If (i%3==1) -> call no. " + i + " to uri:" + request1.getURI()); } else if (i % 3 == 2) { response = client.execute(request2); System.out.println("If (i%3==2) -> call no. " + i + " to uri:" + request2.getURI()); } else { response = client.execute(request); System.out.println("Else -> call no. " + i + " to uri:" + request.getURI()); } BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; System.out.println("call no. " + i); while ((line = rd.readLine()) != null) { System.out.println(line); } } }
From source file:niclients.main.pubni.java
public static void main(String[] args) throws UnsupportedEncodingException { HttpClient client = new DefaultHttpClient(); if (commandparser(args)) { niname = addauthorityToNiname(niname, authority); }/*from w w w. j av a 2 s. c o m*/ niname = niUtils.makenif(niname, filename); if (niname != null) { if (createpub()) { try { HttpResponse response = client.execute(post); int resp_code = response.getStatusLine().getStatusCode(); System.err.println("RESP_CODE: " + Integer.toString(resp_code)); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("Niname creation failed!\n"); } } else { System.out.println("Command line parsing failed!\n"); } }
From source file:org.wso2.carbon.sample.atmstats.ATMTransactionStatsClient.java
public static void main(String[] args) throws XMLStreamException { System.out.println(xmlMsgs.get(1)); System.out.println(xmlMsgs.get(2)); System.out.println(xmlMsgs.get(3)); KeyStoreUtil.setTrustStoreParams();// w w w . j a v a 2 s .co m String url = args[0]; String username = args[1]; String password = args[2]; HttpClient httpClient = new SystemDefaultHttpClient(); try { HttpPost method = new HttpPost(url); for (String xmlElement : xmlMsgs) { StringEntity entity = new StringEntity(xmlElement); method.setEntity(entity); if (url.startsWith("https")) { processAuthentication(method, username, password); } httpClient.execute(method).getEntity().getContent().close(); } Thread.sleep(500); // We need to wait some time for the message to be sent } catch (Throwable t) { t.printStackTrace(); } }
From source file:org.vuphone.vandyupon.test.ImagePostTester.java
public static void main(String[] args) { try {/*from w ww.j ava 2s .c om*/ File image = new File("2008-05-8 ChrisMikeNinaGrad.jpg"); byte[] bytes = new byte[(int) image.length()]; FileInputStream fis = new FileInputStream(image); int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } fis.close(); HttpClient c = new DefaultHttpClient(); HttpPost post = new HttpPost( "http://afrl-gift.dre.vanderbilt.edu:8080/vandyupon/events/?type=eventimagepost&eventid=2" + "&time=" + System.currentTimeMillis() + "&resp=xml"); post.addHeader("Content-Type", "image/jpeg"); ByteArrayEntity bae = new ByteArrayEntity(bytes); post.setEntity(bae); try { HttpResponse resp = c.execute(post); resp.getEntity().writeTo(System.out); } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } }