Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.ap.http;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import org.apache.http.Header;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.methods.*;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- public class HttpMain {
- public static void main(String[] args) throws Exception {
- CloseableHttpClient httpclient = HttpClients.createDefault();
- try {
- // POST
- HttpPost httpPost = new HttpPost("https://nicolas.cloudant.com/http/");
- httpPost.setEntity(new StringEntity("{\"_id\" : \"nicolas_goris\",\"name\" : \"Nicolas Goris\", \"function\": \"Lecturer\"}",
- ContentType.create("application/json")));
- CloseableHttpResponse response2 = httpclient.execute(httpPost);
- try {
- System.out.println(response2.getStatusLine());
- HttpEntity entity2 = response2.getEntity();
- // do something useful with the response body
- // and ensure it is fully consumed
- BufferedReader in = new BufferedReader(
- new InputStreamReader(entity2.getContent()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- //print result
- System.out.println("Response : " + response.toString());
- EntityUtils.consume(entity2);
- }
- finally {
- response2.close();
- System.out.println("**********************************");
- }
- // GET
- HttpGet httpGet = new HttpGet("https://nicolas.cloudant.com/http/nicolas_goris");
- CloseableHttpResponse response1 = httpclient.execute(httpGet);
- try {
- System.out.println(response1.getStatusLine());
- HttpEntity entity1 = response1.getEntity();
- System.out.println("Response to String : " + entity1.toString());
- // do something useful with the response body
- // and ensure it is fully consumed
- BufferedReader in = new BufferedReader(
- new InputStreamReader(entity1.getContent()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- //print result
- System.out.println("Response body : " + response.toString());
- EntityUtils.consume(entity1);
- }
- finally {
- response1.close();
- System.out.println(",<><><><><><><><><><><><><><><><>");
- }
- // HEAD
- HttpHead httpHead = new HttpHead("https://nicolas.cloudant.com/http/nicolas_goris");
- CloseableHttpResponse response3 = httpclient.execute(httpHead);
- try {
- System.out.println(response3.getStatusLine());
- Header[] headers = response3.getAllHeaders();
- for(Header h : headers){
- System.out.println(h.getName() + " : " + h.getValue());
- }
- }
- finally {
- //System.out.println("HEAD OK");
- }
- // DELETE
- /*HttpDelete httpDelete = new HttpDelete("https://nicolas.cloudant.com/http/nicolas_goris?rev=1-5961ccaf23092525e3fe25fe72dddcc6");
- CloseableHttpResponse response4 = httpclient.execute(httpDelete);
- try {
- System.out.println(response4.getStatusLine());
- }
- finally {
- //System.out.println("DELETE OK");
- }*/
- }
- finally {
- httpclient.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement