Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. package edu.ap.http;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. import org.apache.http.Header;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.client.methods.*;
  9. import org.apache.http.entity.ContentType;
  10. import org.apache.http.entity.StringEntity;
  11. import org.apache.http.impl.client.CloseableHttpClient;
  12. import org.apache.http.impl.client.HttpClients;
  13. import org.apache.http.util.EntityUtils;
  14.  
  15. public class HttpMain {
  16.  
  17. public static void main(String[] args) throws Exception {
  18.  
  19. CloseableHttpClient httpclient = HttpClients.createDefault();
  20. try {
  21.  
  22. // POST
  23. HttpPost httpPost = new HttpPost("https://nicolas.cloudant.com/http/");
  24. httpPost.setEntity(new StringEntity("{\"_id\" : \"nicolas_goris\",\"name\" : \"Nicolas Goris\", \"function\": \"Lecturer\"}",
  25. ContentType.create("application/json")));
  26. CloseableHttpResponse response2 = httpclient.execute(httpPost);
  27.  
  28. try {
  29. System.out.println(response2.getStatusLine());
  30. HttpEntity entity2 = response2.getEntity();
  31. // do something useful with the response body
  32. // and ensure it is fully consumed
  33. BufferedReader in = new BufferedReader(
  34. new InputStreamReader(entity2.getContent()));
  35. String inputLine;
  36. StringBuffer response = new StringBuffer();
  37.  
  38. while ((inputLine = in.readLine()) != null) {
  39. response.append(inputLine);
  40. }
  41. in.close();
  42.  
  43. //print result
  44. System.out.println("Response : " + response.toString());
  45. EntityUtils.consume(entity2);
  46. }
  47. finally {
  48. response2.close();
  49. System.out.println("**********************************");
  50. }
  51.  
  52. // GET
  53. HttpGet httpGet = new HttpGet("https://nicolas.cloudant.com/http/nicolas_goris");
  54. CloseableHttpResponse response1 = httpclient.execute(httpGet);
  55.  
  56. try {
  57. System.out.println(response1.getStatusLine());
  58. HttpEntity entity1 = response1.getEntity();
  59. System.out.println("Response to String : " + entity1.toString());
  60. // do something useful with the response body
  61. // and ensure it is fully consumed
  62. BufferedReader in = new BufferedReader(
  63. new InputStreamReader(entity1.getContent()));
  64. String inputLine;
  65. StringBuffer response = new StringBuffer();
  66.  
  67. while ((inputLine = in.readLine()) != null) {
  68. response.append(inputLine);
  69. }
  70. in.close();
  71.  
  72. //print result
  73. System.out.println("Response body : " + response.toString());
  74.  
  75.  
  76. EntityUtils.consume(entity1);
  77. }
  78. finally {
  79. response1.close();
  80. System.out.println(",<><><><><><><><><><><><><><><><>");
  81. }
  82.  
  83. // HEAD
  84. HttpHead httpHead = new HttpHead("https://nicolas.cloudant.com/http/nicolas_goris");
  85. CloseableHttpResponse response3 = httpclient.execute(httpHead);
  86.  
  87. try {
  88. System.out.println(response3.getStatusLine());
  89. Header[] headers = response3.getAllHeaders();
  90. for(Header h : headers){
  91. System.out.println(h.getName() + " : " + h.getValue());
  92. }
  93. }
  94. finally {
  95. //System.out.println("HEAD OK");
  96. }
  97.  
  98. // DELETE
  99. /*HttpDelete httpDelete = new HttpDelete("https://nicolas.cloudant.com/http/nicolas_goris?rev=1-5961ccaf23092525e3fe25fe72dddcc6");
  100. CloseableHttpResponse response4 = httpclient.execute(httpDelete);
  101. try {
  102. System.out.println(response4.getStatusLine());
  103. }
  104. finally {
  105. //System.out.println("DELETE OK");
  106. }*/
  107. }
  108. finally {
  109. httpclient.close();
  110. }
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement