List of usage examples for org.apache.http.client HttpClient execute
HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;
From source file:com.oDesk.api.oDeskRestClient.java
/** * Execute GET request/*from w w w. j av a 2 s.c o m*/ * * @param url Request object for GET * @param method HTTP method * @param params POST parameters * @throws JSONException * @return {@link JSONObject} * */ private static JSONObject doGetRequest(HttpGet httpGet) throws JSONException { JSONObject json = null; HttpClient httpClient = HttpClientBuilder.create().build(); HttpResponse response; try { response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result = convertStreamToString(instream); instream.close(); json = new JSONObject(result); } } else { json = oDeskRestClient.genError(response); } } catch (ClientProtocolException e) { json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException"); } catch (IOException e) { json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException"); } catch (JSONException e) { json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException"); } catch (Exception e) { json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString()); } finally { httpGet.abort(); } return json; }
From source file:com.Upwork.api.UpworkRestClient.java
/** * Execute GET request//from w w w .j a v a 2s .co m * * @param url Request object for GET * @param method HTTP method * @param params POST parameters * @throws JSONException * @return {@link JSONObject} * */ private static JSONObject doGetRequest(HttpGet httpGet) throws JSONException { JSONObject json = null; HttpClient httpClient = HttpClientBuilder.create().build(); HttpResponse response; try { response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result = convertStreamToString(instream); instream.close(); json = new JSONObject(result); } } else { json = UpworkRestClient.genError(response); } } catch (ClientProtocolException e) { json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException"); } catch (IOException e) { json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException"); } catch (JSONException e) { json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException"); } catch (Exception e) { json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString()); } finally { httpGet.abort(); } return json; }
From source file:com.oDesk.api.oDeskRestClient.java
/** * Execute POST request/*www. j av a 2 s . c o m*/ * * @param url Request object for POST * @param method HTTP method * @param params POST parameters * @throws JSONException * @return {@link JSONObject} * */ private static JSONObject doPostRequest(HttpPost httpPost, HashMap<String, String> params) throws JSONException { JSONObject json = null; HttpClient postClient = HttpClientBuilder.create().build(); HttpResponse response; try { response = postClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result = convertStreamToString(instream); instream.close(); json = new JSONObject(result); } } else { json = oDeskRestClient.genError(response); } } catch (ClientProtocolException e) { json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException"); } catch (IOException e) { json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException"); } catch (JSONException e) { json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException"); } catch (Exception e) { json = oDeskRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString()); } finally { httpPost.abort(); } return json; }
From source file:com.Upwork.api.UpworkRestClient.java
/** * Execute POST request//from w w w .ja v a2 s . c om * * @param url Request object for POST * @param method HTTP method * @param params POST parameters * @throws JSONException * @return {@link JSONObject} * */ private static JSONObject doPostRequest(HttpPost httpPost, HashMap<String, String> params) throws JSONException { JSONObject json = null; HttpClient postClient = HttpClientBuilder.create().build(); HttpResponse response; try { response = postClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result = convertStreamToString(instream); instream.close(); json = new JSONObject(result); } } else { json = UpworkRestClient.genError(response); } } catch (ClientProtocolException e) { json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException"); } catch (IOException e) { json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: IOException"); } catch (JSONException e) { json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: JSONException"); } catch (Exception e) { json = UpworkRestClient.genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString()); } finally { httpPost.abort(); } return json; }
From source file:org.keycloak.example.oauth.ProductDatabaseClient.java
public static List<String> getProducts(HttpServletRequest request, String accessToken) throws Failure { KeycloakSecurityContext session = (KeycloakSecurityContext) request .getAttribute(KeycloakSecurityContext.class.getName()); // The ServletOAuthClient is obtained by getting a context attribute // that is set in the Bootstrap context listener in this project. // You really should come up with a better way to initialize // and obtain the ServletOAuthClient. I actually suggest downloading the ServletOAuthClient code // and take a look how it works. You can also take a look at third-party-cdi example ServletOAuthClient oAuthClient = (ServletOAuthClient) request.getServletContext() .getAttribute(ServletOAuthClient.class.getName()); HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(UriUtils.getOrigin(request.getRequestURL().toString()) + "/database/products"); get.addHeader("Authorization", "Bearer " + accessToken); try {/*from w w w. j a va2 s . com*/ HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() != 200) { throw new Failure(response.getStatusLine().getStatusCode()); } HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); try { return JsonSerialization.readValue(is, TypedList.class); } finally { is.close(); } } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.byteridge.bookcircle.utils.ResponseParser.java
public static List<UserShelf> GetShelvesForUser(String userId) throws Exception { Uri.Builder builder = new Uri.Builder(); builder.scheme("http"); builder.authority("www.goodreads.com"); builder.path("shelf/list"); builder.appendQueryParameter("format", "xml"); builder.appendQueryParameter("user_id", userId); builder.appendQueryParameter("key", _ConsumerKey); HttpClient httpClient = new DefaultHttpClient(); HttpGet getShelvesRequest = new HttpGet(builder.build().toString()); if (get_IsAuthenticated()) { _Consumer.sign(getShelvesRequest); }/*w ww .j a v a2 s .c o m*/ HttpResponse shelvesResponse = httpClient.execute(getShelvesRequest); Response shelvesResponseData = ResponseParser.parse(shelvesResponse.getEntity().getContent()); return shelvesResponseData.get_Shelves().get_UserShelves(); }
From source file:com.buddycloud.friendfinder.HttpUtils.java
public static void post(String URL, Map<String, String> params) throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(URL); HttpParams httpParams = new BasicHttpParams(); for (Entry<String, String> entryParam : params.entrySet()) { httpParams.setParameter(entryParam.getKey(), entryParam.getValue()); }//from www . j a v a2s.co m httpPost.setParams(httpParams); client.execute(httpPost); }
From source file:com.geertvanderploeg.kiekeboek.client.NetworkUtilities.java
/** * Connects to the server, authenticates the provided username and * password./*from w w w .ja va 2 s. c o m*/ * * @param username The user's username * @param password The user's password * @param handler The hander instance from the calling UI thread. * @param context The context of the calling Activity. * @return boolean The boolean result indicating whether the user was * successfully authenticated. */ public static boolean authenticate(String username, String password, Handler handler, final Context context) { final HttpResponse resp; final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("_method", "POST")); params.add(new BasicNameValuePair(PARAM_USERNAME, username)); params.add(new BasicNameValuePair(PARAM_PASSWORD, password)); HttpEntity entity = null; try { entity = new UrlEncodedFormEntity(params); } catch (final UnsupportedEncodingException e) { // this should never happen. throw new AssertionError(e); } final HttpPost post = new HttpPost(context.getString(AUTH_URI_RESOURCE)); post.addHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded")); post.setEntity(entity); HttpClient localHttpClient = getHttpClient(); Log.d(TAG, "POST-ing params to URL '" + context.getString(AUTH_URI_RESOURCE) + "': " + params); try { resp = localHttpClient.execute(post); Log.d(TAG, "Authentication response status line: " + resp.getStatusLine()); Log.d(TAG, "Authentication response headers: " + Arrays.asList(resp.getAllHeaders())); if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY && resp.getHeaders("Location") != null && resp.getHeaders("Location")[0].getValue().contains("/intranet/people")) { Log.v(TAG, "Successful authentication"); sendResult(true, handler, context); resp.getEntity().consumeContent(); return true; } else { Log.v(TAG, "Error authenticating " + resp.getStatusLine()); sendResult(false, handler, context); resp.getEntity().consumeContent(); return false; } } catch (final IOException e) { Log.v(TAG, "IOException when getting authtoken", e); sendResult(false, handler, context); return false; } finally { Log.v(TAG, "getAuthtoken completing"); } }
From source file:com.limewoodmedia.nsdroid.LoadingHelper.java
public static Bitmap loadFlag(String url, Context context) throws ClientProtocolException, IOException { if (url == null || url.length() == 0) { // No flag to load return null; }// ww w. jav a 2s. c o m HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, API.getInstance(context).getUserAgent()); client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); InputStream stream = response.getEntity().getContent(); return BitmapFactory.decodeStream(stream); }
From source file:org.wso2.carbon.apimgt.authenticator.oidc.util.Util.java
public static boolean verifySignature(SignedJWT signedIdToken, ServerConfiguration serverConfiguration) throws IOException, ParseException, NoSuchAlgorithmException, InvalidKeySpecException { boolean isSigValid = false; HttpClient httpClient = new DefaultHttpClient(); HttpGet get = new HttpGet(serverConfiguration.getJwksUri()); HttpResponse response = httpClient.execute(get); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String jsonString = ""; String line;//from ww w . j av a2 s. co m while ((line = bufferedReader.readLine()) != null) { jsonString = jsonString + line; } JWKSet jwkSet = JWKSet.parse(jsonString); // map of identifier to verifier Map<String, JWSVerifier> verifiers = new HashMap<String, JWSVerifier>(); List<JWK> jwkList = jwkSet.getKeys(); for (JWK jwkKey : jwkList) { if (jwkKey != null && jwkKey.getKeyID() != null) { // use the key ID that's built into the key itself // TODO (#641): deal with JWK thumbprints //this.keys.put(key.getKeyID(), key); if (jwkKey instanceof RSAKey) { // build RSA signers & verifiers //if (jwkKey.isPrivate()) { // only add the signer if there's a private key //RSASSASigner signer = new RSASSASigner(((RSAKey) jwkKey).toRSAPrivateKey()); //signers.put(id, signer); //} RSASSAVerifier verifier = new RSASSAVerifier(((RSAKey) jwkKey).toRSAPublicKey()); String id = jwkKey.getKeyID(); verifiers.put(id, verifier); } } } for (JWSVerifier verifier : verifiers.values()) { try { if (signedIdToken.verify(verifier)) { isSigValid = true; } } catch (JOSEException e) { log.error("Failed to validate signature, error was: ", e); } } return isSigValid; }