List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient
public DefaultHttpClient()
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;// w w w . j av a2s .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:com.everm.httpclient.HttpClientTest.java
/** * @param args/*from ww w .ja v a 2 s . 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: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 {/*ww w. j a 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:web.restful.ClientTest.java
public static void main(String[] args) throws ClientProtocolException, IOException { HttpClient client = new DefaultHttpClient(); HttpPut put = new HttpPut("http://localhost:8080/ss16-lab-web/resources/outliers/session"); put.setEntity(new StringEntity("upenkwbq"));// session ID client.execute(put);/*from w w w. j a va 2s . c o m*/ put.releaseConnection(); put = new HttpPut("http://localhost:8080/ss16-lab-web/resources/outliers/bucket"); put.setEntity(new StringEntity("Level1/Level1_Bin_1.txt")); // bucket name client.execute(put); put.releaseConnection(); put = new HttpPut("http://localhost:8080/ss16-lab-web/resources/outliers/method"); put.setEntity(new StringEntity("chauvenet")); // method name client.execute(put); put.releaseConnection(); HttpGet get = new HttpGet("http://localhost:8080/ss16-lab-web/resources/outliers"); HttpResponse response = client.execute(get); HttpEntity en = response.getEntity(); InputStreamReader i = new InputStreamReader(en.getContent()); BufferedReader rd = new BufferedReader(i); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); } }
From source file:org.dineth.shooter.client.Example.java
public static void main(String[] args) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost:8080/upload"); filename = "router.jpg"; FileBody bin = new FileBody(new File(filename)); StringBody fileName = null;//from w w w.ja va 2 s .c o m try { fileName = new StringBody(filename); } catch (UnsupportedEncodingException ex) { Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex); } MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("file", bin); reqEntity.addPart("filename", fileName); httppost.setEntity(reqEntity); HttpResponse response = null; try { response = httpclient.execute(httppost); } catch (IOException ex) { Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex); } HttpEntity resEntity = response.getEntity(); }
From source file:RestGetClient.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet("http://localhost:10080/example/json/product/get"); getRequest.addHeader("accept", "application/json"); HttpResponse response = httpClient.execute(getRequest); //if (response.getStatusLine().getStatusCode() != 200) { // throw new RuntimeException("Failed : HTTP error code : " // + response.getStatusLine().getStatusCode()); //}/*from w ww .j av a2 s.c om*/ 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.corfudb.sharedlog.examples.ConfigClnt.java
public static void main(String[] args) throws IOException { DefaultHttpClient httpclient = new DefaultHttpClient(); final BufferedReader prompt = new BufferedReader(new InputStreamReader(System.in)); CorfuConfiguration C = null;//w w w . j a va 2 s .c om while (true) { System.out.print("> "); String line = prompt.readLine(); if (line.startsWith("get")) { HttpGet httpget = new HttpGet("http://localhost:8000/corfu"); System.out.println("Executing request: " + httpget.getRequestLine()); HttpResponse response = (HttpResponse) httpclient.execute(httpget); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); // response.getEntity().writeTo(System.out); // System.out.println(); // System.out.println("----------------------------------------"); C = new CorfuConfiguration(response.getEntity().getContent()); } else { if (C == null) { System.out.println("configuration not set yet!"); continue; } HttpPost httppost = new HttpPost("http://localhost:8000/corfu"); httppost.setEntity(new StringEntity(C.ConfToXMLString())); System.out.println("Executing request: " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); response.getEntity().writeTo(System.out); } } // httpclient.close(); }
From source file:httpclientsample.HttpClientSample.java
/** * @param args the command line arguments *///from ww w . j a v a 2 s. c om public static void main(String[] args) throws IOException, JSONException { /** METHOD GET EXAMPLE **/ DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet("http://localhost:8000/test/api?id=100"); getRequest.addHeader("accept", "application/json"); HttpResponse response = httpClient.execute(getRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } InputStreamReader isr = new InputStreamReader((response.getEntity().getContent())); BufferedReader br = new BufferedReader(isr); String output; System.out.println("Response:\n"); while ((output = br.readLine()) != null) { JSONObject jsonObj = new JSONObject(output); System.out.println("json id : " + jsonObj.get("id")); } httpClient.getConnectionManager().shutdown(); }
From source file:RestPostClient.java
public static void main(String[] args) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("http://localhost:10080/example/json/product/post"); StringEntity input = new StringEntity("{\"qty\":100,\"name\":\"iPad 4\"}"); input.setContentType("application/json"); postRequest.setEntity(input);// w w w. ja va 2 s .co m HttpResponse response = httpClient.execute(postRequest); //if (response.getStatusLine().getStatusCode() != 201) { // throw new RuntimeException("Failed : HTTP error code : " // + 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:at.orz.arangodb.sandbox.PostChunkTest.java
/** * @param args//from www . j a va2 s . c o m */ public static void main(String[] args) throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost( "http://arango-test-server:9999/_api/import?collection=test1&createCollection=true&type=documents"); //post.setEntity(new StringEntity("{\"xx\": \"123\"}{\"xx\": \"456\"}")); InputStreamEntity entity = new InputStreamEntity( new ByteArrayInputStream("{\"xx\": \"123\"}{\"xx\": \"456\"}".getBytes()), 26); entity.setChunked(true); post.setEntity(entity); HttpResponse res = client.execute(post); System.out.println(res.getStatusLine()); post.releaseConnection(); }