Java Code Examples for org.apache.http.impl.client.DefaultHttpClient.close()

The following are Jave code examples for showing how to use close() of the org.apache.http.impl.client.DefaultHttpClient class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: twasi-core   File: TwitchAPI.java   View Source Code Vote up 6 votes
public AccessTokenDTO getToken(String code) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    AccessTokenDTO token = null;
    try {
        HttpPost httppost = new HttpPost("https://api.twitch.tv/kraken/oauth2/token" +
                "?client_id=" + Config.getCatalog().twitch.clientId +
                "&client_secret=" + Config.getCatalog().twitch.clientSecret +
                "&code=" + code +
                "&grant_type=authorization_code" +
                "&redirect_uri=" + Config.getCatalog().twitch.redirectUri);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);

        token = new Gson().fromJson(responseBody, AccessTokenDTO.class);
    } catch (IOException e) {
        TwasiLogger.log.error(e);
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
        httpclient.close();
    }
    return token;
}
 
Example 2
Project: aliyun-cupid-sdk   File: WebProxyCall.java   View Source Code Vote up 5 votes
public void callWebProxy(String url) {
    String resultCode = "";
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            resultCode = ResponseCode.CALLRESPONSEERROR;
            if (entity != null) {
                String responseString = EntityUtils.toString(entity);
                if (responseString.contains("Spark Jobs") && responseString.contains("Stages")
                        && responseString.contains("Storage") && responseString.contains("Environment")
                        && responseString.contains("Executors")) {
                    resultCode = ResponseCode.CALLSUCCESS;
                }
            }
        } else if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY
                || statusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
            resultCode = ResponseCode.CALLFORBIDDEN;
        } else {
            resultCode = ResponseCode.OTHER_RESPONSE + String.valueOf(statusCode);
        }
    } catch (Exception e) {
        LOG.warn("WebProxyCall exception " + e.getMessage());
        resultCode = ResponseCode.CALLEXCEPTION;
    } finally {
        httpclient.close();
    }
    LOG.info("WebProxyCall result " + resultCode);
    if (!resultCode.equals(ResponseCode.CALLSUCCESS)) {
        System.exit(1);
    }
}
 
Example 3
Project: twasi-core   File: TwitchAPI.java   View Source Code Vote up 5 votes
public TwitchAccount getTwitchAccountByToken(AccessTokenDTO token) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    TwitchAccount acc = null;
    try {
        HttpGet httpget = new HttpGet("https://api.twitch.tv/kraken");

        httpget.setHeader("Accept", "application/vnd.twitchtv.v5+json");
        httpget.setHeader("Authorization", "OAuth " + token.getAccessToken());

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);

        TokenInfoDTO info = new Gson().fromJson(responseBody, TokenInfoDTO.class);

        acc = new TwitchAccount(info.getToken().getUserName(), token.toModel(), info.getToken().getUserId(), new ArrayList<>());
        acc.setEmail(info.getToken().getEmail());
    } catch (IOException e) {
        TwasiLogger.log.error(e);
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
        httpclient.close();
    }
    return acc;
}
 
Example 4
Project: twasi-core   File: TwitchAPI.java   View Source Code Vote up 5 votes
public void applyUserInfo(TwitchAccount account) {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    TwitchAccount acc = null;
    try {
        HttpGet httpget = new HttpGet("https://api.twitch.tv/kraken/user");

        httpget.setHeader("Accept", "application/vnd.twitchtv.v5+json");
        httpget.setHeader("Authorization", "OAuth " + account.getToken().getAccessToken());

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);

        System.out.println(responseBody);

        UserInfoDTO info = new Gson().fromJson(responseBody, UserInfoDTO.class);

        account.setEmail(info.getEmail());
    } catch (IOException e) {
        TwasiLogger.log.error(e);
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
        httpclient.close();
    }
}