Package org.apache.http.client.methods

Examples of org.apache.http.client.methods.CloseableHttpResponse


        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet("http://www.apache.org/");

            System.out.println("Executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                // Do not feel like reading the response body
                // Call abort on the request object
                httpget.abort();
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here


            // FileEntity entity = new FileEntity(file, "binary/octet-stream");

            httppost.setEntity(reqEntity);

            System.out.println("Executing request: " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

            // precedence over those set at the client level.
            context.setCookieStore(cookieStore);
            context.setCredentialsProvider(credentialsProvider);

            System.out.println("executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget, context);
            try {
                HttpEntity entity = response.getEntity();

                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    System.out.println("Response content length: " + entity.getContentLength());
                }
                System.out.println("----------------------------------------");

                // Once the request has been executed the local context can
                // be used to examine updated state and various objects affected
                // by the request execution.

                // Last executed request
                context.getRequest();
                // Execution route
                context.getHttpRoute();
                // Target auth state
                context.getTargetAuthState();
                // Proxy auth state
                context.getTargetAuthState();
                // Cookie origin
                context.getCookieOrigin();
                // Cookie spec used
                context.getCookieSpec();
                // User security token
                context.getUserToken();

            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet("http://localhost/");

            System.out.println("Executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());

                // Get hold of the response entity
                HttpEntity entity = response.getEntity();

                // If the response does not enclose an entity, there is no need
                // to bother about connection release
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    try {
                        instream.read();
                        // do something useful with the response
                    } catch (IOException ex) {
                        // In case of an IOException the connection will be released
                        // back to the connection manager automatically
                        throw ex;
                    } finally {
                        // Closing the input stream will trigger connection release
                        instream.close();
                    }
                }
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

            HttpHost target = new HttpHost("localhost", 80, "http");
            HttpGet request = new HttpGet("/");

            System.out.println("Executing request " + request + " to " + target + " via SOCKS proxy " + socksaddr);
            CloseableHttpResponse response = httpclient.execute(target, request);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

        // Windows platform specific methods via JNI.
        try {
            HttpGet httpget = new HttpGet("http://winhost/");

            System.out.println("Executing request " + httpget.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

            HttpGet httpget = new HttpGet("/");

            System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
            for (int i = 0; i < 3; i++) {
                CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
                try {
                    System.out.println("----------------------------------------");
                    System.out.println(response.getStatusLine());
                    EntityUtils.consume(response.getEntity());
                } finally {
                    response.close();
                }
            }
        } finally {
            httpclient.close();
        }
View Full Code Here

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost tokenPost = new HttpPost(TOKEN_URL);
        tokenPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
        tokenPost.setEntity(new StringEntity(entity, "UTF-8"));
        CloseableHttpResponse resp1 = httpclient.execute(tokenPost);
        ObjectReader jreader = Utils.getJsonReader(Map.class);

        if (resp1 != null && resp1.getEntity() != null) {
          Map<String, Object> token = jreader.readValue(resp1.getEntity().getContent());
          if (token != null && token.containsKey("access_token")) {
            // got valid token
            HttpGet profileGet = new HttpGet(PROFILE_URL);
            profileGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token.get("access_token"));
            CloseableHttpResponse resp2 = httpclient.execute(profileGet);
            HttpEntity respEntity = resp2.getEntity();
            String ctype = resp2.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();

            if (respEntity != null && Utils.isJsonType(ctype)) {
              Map<String, Object> profile = jreader.readValue(resp2.getEntity().getContent());

              if (profile != null && profile.containsKey("sub")) {
                String googleSubId = (String) profile.get("sub");
                String pic = (String) profile.get("picture");
                String email = (String) profile.get("email");
                String name = (String) profile.get("name");

                user.setIdentifier(Config.GPLUS_PREFIX.concat(googleSubId));
                user = User.readUserForIdentifier(user);
                if (user == null) {
                  //user is new
                  user = new User();
                  user.setEmail(StringUtils.isBlank(email) ? "email@domain.com" : email);
                  user.setName(StringUtils.isBlank(name) ? "No Name" : name);
                  user.setPassword(new UUID().toString());
                  user.setIdentifier(Config.GPLUS_PREFIX.concat(googleSubId));
                  if (user.getPicture() == null) {
                    if (pic != null) {
                      if (pic.indexOf("?") > 0) {
                        // user picture migth contain size parameters - remove them
                        user.setPicture(pic.substring(0, pic.indexOf("?")));
                      } else {
                        user.setPicture(pic);
                      }
                    } else {
                      user.setPicture("http://www.gravatar.com/avatar?d=mm&size=200");
                    }
                  }

                  String id = user.create();
                  if (id == null) {
                    throw new AuthenticationServiceException("Authentication failed: cannot create new user.");
                  }
                }
                userAuth = new UserAuthentication(user);
              }
              EntityUtils.consumeQuietly(resp2.getEntity());
            }
            EntityUtils.consumeQuietly(resp1.getEntity());
          }
        }
      }
View Full Code Here

        String url = Utils.formatMessage(TOKEN_URL, authCode,
            request.getRequestURL().toString(), Config.LINKEDIN_APP_ID, Config.LINKEDIN_SECRET);

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost tokenPost = new HttpPost(url);
        CloseableHttpResponse resp1 = httpclient.execute(tokenPost);
        ObjectReader jreader = Utils.getJsonReader(Map.class);

        if (resp1 != null && resp1.getEntity() != null) {
          Map<String, Object> token = jreader.readValue(resp1.getEntity().getContent());
          if (token != null && token.containsKey("access_token")) {
            // got valid token
            HttpGet profileGet = new HttpGet(PROFILE_URL + token.get("access_token"));
            CloseableHttpResponse resp2 = httpclient.execute(profileGet);
            HttpEntity respEntity = resp2.getEntity();
            String ctype = resp2.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();

            if (respEntity != null && Utils.isJsonType(ctype)) {
              Map<String, Object> profile = jreader.readValue(resp2.getEntity().getContent());

              if (profile != null && profile.containsKey("id")) {
                String linkedInID = (String) profile.get("id");
                String email = (String) profile.get("emailAddress");
                String pic = (String) profile.get("pictureUrl");
                String fName = (String) profile.get("firstName");
                String lName = (String) profile.get("lastName");
                String name = fName + " " + lName;

                user.setIdentifier(Config.LINKEDIN_PREFIX.concat(linkedInID));
                user = User.readUserForIdentifier(user);
                if (user == null) {
                  //user is new
                  user = new User();
                  user.setEmail(StringUtils.isBlank(email) ? "email@domain.com" : email);
                  user.setName(StringUtils.isBlank(name) ? "No Name" : name);
                  user.setPassword(new UUID().toString());
                  user.setIdentifier(Config.LINKEDIN_PREFIX.concat(linkedInID));
                  if (user.getPicture() == null) {
                    if (pic != null) {
                      user.setPicture(pic);
                    } else {
                      user.setPicture("http://www.gravatar.com/avatar?d=mm&size=200");
                    }
                  }

                  String id = user.create();
                  if (id == null) {
                    throw new AuthenticationServiceException("Authentication failed: cannot create new user.");
                  }
                }
                userAuth = new UserAuthentication(user);
              }
              EntityUtils.consumeQuietly(resp2.getEntity());
            }
            EntityUtils.consumeQuietly(resp1.getEntity());
          }
        }
      }
View Full Code Here

        String url = Utils.formatMessage(TOKEN_URL, authCode,
            request.getRequestURL().toString(), Config.FB_APP_ID, Config.FB_SECRET);

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet tokenPost = new HttpGet(url);
        CloseableHttpResponse resp1 = httpclient.execute(tokenPost);
        ObjectReader jreader = Utils.getJsonReader(Map.class);

        if (resp1 != null && resp1.getEntity() != null) {
          String token = EntityUtils.toString(resp1.getEntity(), Config.DEFAULT_ENCODING);
          if (token != null && token.startsWith("access_token")) {
            // got valid token
            String accessToken = token.substring(token.indexOf("=") + 1, token.indexOf("&"));
            HttpGet profileGet = new HttpGet(PROFILE_URL + accessToken);
            CloseableHttpResponse resp2 = httpclient.execute(profileGet);
            HttpEntity respEntity = resp2.getEntity();
            String ctype = resp2.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();

            if (respEntity != null && Utils.isJsonType(ctype)) {
              Map<String, Object> profile = jreader.readValue(resp2.getEntity().getContent());

              if (profile != null && profile.containsKey("id")) {
                String fbId = (String) profile.get("id");
                Map<String, Object> pic = (Map<String, Object>) profile.get("picture");
                String email = (String) profile.get("email");
                String name = (String) profile.get("name");

                user.setIdentifier(Config.FB_PREFIX.concat(fbId));
                user = User.readUserForIdentifier(user);
                if (user == null) {
                  //user is new
                  user = new User();
                  user.setEmail(StringUtils.isBlank(email) ? "email@domain.com" : email);
                  user.setName(StringUtils.isBlank(name) ? "No Name" : name);
                  user.setPassword(new UUID().toString());
                  user.setIdentifier(Config.FB_PREFIX.concat(fbId));
                  if (user.getPicture() == null && pic != null) {
                    Map<String, Object> data = (Map<String, Object>) pic.get("data");
                    // try to get the direct url to the profile pic
                    if (data != null && data.containsKey("url")) {
                      user.setPicture((String) data.get("url"));
                    } else {
                      user.setPicture("http://graph.facebook.com/" + fbId +
                          "/picture?width=400&height=400&type=square");
                    }
                  }

                  String id = user.create();
                  if (id == null) {
                    throw new AuthenticationServiceException("Authentication failed: cannot create new user.");
                  }
                }
                userAuth = new UserAuthentication(user);
              }
              EntityUtils.consumeQuietly(resp2.getEntity());
            }
            EntityUtils.consumeQuietly(resp1.getEntity());
          }
        }
      }
View Full Code Here

TOP

Related Classes of org.apache.http.client.methods.CloseableHttpResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.