Example usage for org.apache.http.impl.client DefaultHttpClient execute

List of usage examples for org.apache.http.impl.client DefaultHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient execute.

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:att.jaxrs.client.Library.java

public static String deleteLibrary(long content_id) {
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("content_id", Long.toString(content_id)));

    String resultStr = "";

    try {//  w  ww  . j  a  v a  2  s  . c  o m

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.DELETE_LIBRARY_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result.getEntity().getContent());
        System.out.println(Constants.RESPONSE_BODY + resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    return resultStr;
}

From source file:att.jaxrs.client.Library.java

public static String updateLibrary(Library library) {

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("category_id", Integer.toString(library.getCategory_id())));
    urlParameters.add(new BasicNameValuePair("title", library.getTitle()));
    urlParameters.add(new BasicNameValuePair("published_date", library.getPublished_date()));
    urlParameters.add(new BasicNameValuePair("url", library.getUrl()));

    String resultStr = "";

    try {/*  w ww  . ja  va 2s . c om*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.UPDATE_LIBRARY_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result);
        System.out.println(Constants.RESPONSE_BODY + resultStr);
        System.out.println("testing $$$$$$$$$$$$$$$$$$$$$$");
    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    return resultStr;
}

From source file:org.zaizi.oauth2utils.OAuthUtils.java

public static void getProtectedResource(Properties config) {
    String resourceURL = config.getProperty(OAuthConstants.RESOURCE_SERVER_URL);
    OAuth2Details oauthDetails = createOAuthDetails(config);
    HttpGet get = new HttpGet(resourceURL);
    get.addHeader(OAuthConstants.AUTHORIZATION,
            getAuthorizationHeaderForAccessToken(oauthDetails.getAccessToken()));
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = null;//from   w w w .jav a  2  s .c o m
    int code = -1;
    try {
        response = client.execute(get);
        code = response.getStatusLine().getStatusCode();
        if (code >= 400) {
            // Access token is invalid or expired.Regenerate the access
            // token
            System.out.println("Access token is invalid or expired. Regenerating access token....");
            String accessToken = getAccessToken(oauthDetails);
            if (isValid(accessToken)) {
                // update the access token
                // System.out.println("New access token: " + accessToken);
                oauthDetails.setAccessToken(accessToken);
                get.removeHeaders(OAuthConstants.AUTHORIZATION);
                get.addHeader(OAuthConstants.AUTHORIZATION,
                        getAuthorizationHeaderForAccessToken(oauthDetails.getAccessToken()));
                get.releaseConnection();
                response = client.execute(get);
                code = response.getStatusLine().getStatusCode();
                if (code >= 400) {
                    throw new RuntimeException(
                            "Could not access protected resource. Server returned http code: " + code);

                }

            } else {
                throw new RuntimeException("Could not regenerate access token");
            }

        }

        handleResponse(response);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        get.releaseConnection();
    }
}

From source file:att.jaxrs.client.Library.java

public static long getExistingRecord(String title, int category_id) {

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("title", title));
    urlParameters.add(new BasicNameValuePair("category_id", Long.valueOf(category_id).toString()));

    HttpResponse result;/*  w  ww .java  2s .c om*/
    String resultStr;
    LibraryCollection libraryCollection = new LibraryCollection();

    try {
        System.out.println("invoking getExistingRecord, Title: " + title + ", Category ID: " + category_id);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.SELECT_LAST_ADDED_LIBRARY_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        resultStr = Util.getStringFromInputStream(result);

        libraryCollection = Marshal.unmarshal(LibraryCollection.class, resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();

    }

    if (null != libraryCollection.getLibrary()) {
        return libraryCollection.getLibrary()[0].getContent_id();
    }
    return -1;
}

From source file:com.ichi2.anki.AnkiDroidProxy.java

/**
 * Get shared decks.//from w  w  w . j  a v a  2 s  .  co m
 */
public static List<SharedDeck> getSharedDecks() throws Exception {

    try {
        if (sSharedDecks == null) {
            sSharedDecks = new ArrayList<SharedDeck>();

            HttpGet httpGet = new HttpGet(SYNC_SEARCH);
            httpGet.setHeader("Accept-Encoding", "identity");
            httpGet.setHeader("Host", SYNC_HOST);
            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();

            HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
            String response = Utils.convertStreamToString(httpResponse.getEntity().getContent());
            // Log.i(AnkiDroidApp.TAG, "Content = " + response);
            sSharedDecks.addAll(getSharedDecksListFromJSONArray(new JSONArray(response)));
        }
    } catch (Exception e) {
        sSharedDecks = null;
        throw new Exception();
    }

    return sSharedDecks;
}

From source file:se.vgregion.util.HTTPUtils.java

public static HttpResponse makePostXML(String url, String token, DefaultHttpClient client, String xml)
        throws Exception {
    HttpPost post = new HttpPost(url);

    StringEntity e = new StringEntity(xml, "utf-8");
    e.setContentType("application/xml");
    post.addHeader("X-TrackerToken", token);
    post.addHeader("Content-type", "application/xml");
    post.setEntity(e);//w  w  w  . j av a2  s.c  o m

    return client.execute(post);
}

From source file:com.wareninja.opensource.gravatar4android.common.Utils.java

public static String downloadProfile(String profileUrl) throws GenericException {

    String response = "";

    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, TIMEOUT);

    DefaultHttpClient httpClient = new DefaultHttpClient(myParams);

    HttpGet httpGet = new HttpGet(profileUrl);
    if (CONSTANTS.DEBUG)
        Log.d(TAG, "WebGetURL: " + profileUrl);

    HttpResponse httpResponse = null;//  ww w.  j a  v a  2 s.  co m
    try {
        httpResponse = httpClient.execute(httpGet);

        response = EntityUtils.toString(httpResponse.getEntity());
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        throw new GenericException(e);
    }

    return response;
}

From source file:att.jaxrs.client.Library.java

public static Library selectWithKeyLibraryResource(long content_id) {
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("content_id", Long.toString(content_id)));

    LibraryCollection libraryCollection = new LibraryCollection();
    try {/*w ww  .j a va  2 s  .  com*/

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(Constants.SELECT_WITH_KEY_LIBRARY_RESOURCE);
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse result = httpClient.execute(post);
        System.out.println(Constants.RESPONSE_STATUS_CODE + result);
        String resultStr = Util.getStringFromInputStream(result);
        System.out.println(Constants.RESPONSE_BODY + resultStr);
        System.out.println("##################################");

        libraryCollection = Marshal.unmarshal(LibraryCollection.class, resultStr);

    } catch (Exception e) {
        System.out.println(e.getMessage());

    }
    if (libraryCollection.getLibrary().length == 1) {
        return libraryCollection.getLibrary()[0];
    }
    return null;
}

From source file:nl.mpcjanssen.simpletask.util.Util.java

public static InputStream getInputStreamFromUrl(String url) throws ClientProtocolException, IOException {
    HttpGet request = new HttpGet(url);
    DefaultHttpClient client = new DefaultHttpClient(getTimeoutHttpParams());
    HttpResponse response = client.execute(request);
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != 200) {
        Log.e(TAG, "Failed to get stream for: " + url);
        throw new IOException("Failed to get stream for: " + url);
    }/*from   www  .jav  a 2 s.c  om*/
    return response.getEntity().getContent();
}

From source file:com.todotxt.todotxttouch.util.Util.java

public static InputStream getInputStreamFromUrl(String url) throws ClientProtocolException, IOException {
    HttpGet request = new HttpGet(url);
    DefaultHttpClient client = new DefaultHttpClient(getTimeoutHttpParams());
    HttpResponse response = client.execute(request);
    int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode != 200) {
        Log.e(TAG, "Failed to get stream for: " + url);
        throw new IOException("Failed to get stream for: " + url);
    }//www .j  a  v a 2 s.  com

    return response.getEntity().getContent();
}