Example usage for org.apache.http.client HttpClient execute

List of usage examples for org.apache.http.client HttpClient execute

Introduction

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

Prototype

HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;

Source Link

Document

Executes HTTP request using the default context.

Usage

From source file:og.android.tether.system.WebserviceTask.java

public static HttpResponse makeRequest(String url, List<BasicNameValuePair> params)
        throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    String paramString = URLEncodedUtils.format(params, "utf-8");
    Log.d(MSG_TAG, url + "?" + paramString);
    HttpGet request = new HttpGet(url + "?" + paramString);
    return client.execute(request);
}

From source file:com.drc.wiktionary.SimpleWikiHelper.java

/**
 * Pull the raw text content of the given URL. This call blocks until the
 * operation has completed, and is synchronized because it uses a shared
 * buffer {@link #sBuffer}.// w  ww  . ja v a2  s .com
 * 
 * @param url The exact URL to request.
 * @return The raw content returned by the server.
 * @throws ApiException If any connection or server error occurs.
 */
protected static synchronized String getUrlContent(String url) throws ApiException {
    if (sUserAgent == null) {
        throw new ApiException("User-Agent string must be prepared");
    }

    // Create client and set our specific user-agent string
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    request.setHeader("User-Agent", sUserAgent);

    try {
        HttpResponse response = client.execute(request);

        // Check if server response is valid
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != HTTP_STATUS_OK) {
            throw new ApiException("Invalid response from server: " + status.toString());
        }

        // Pull content stream from response
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();

        ByteArrayOutputStream content = new ByteArrayOutputStream();

        // Read response into a buffered stream
        int readBytes = 0;
        while ((readBytes = inputStream.read(sBuffer)) != -1) {
            content.write(sBuffer, 0, readBytes);
        }

        // Return result from buffered stream
        return new String(content.toByteArray(), ENCODING);

    } catch (IOException e) {
        throw new ApiException("Problem communicating with API", e);
    }
}

From source file:aajavafx.LoginController.java

public static String getPassword(String userName) throws MalformedURLException, JSONException, IOException {
    String password = "";

    Managers manager = new Managers();
    Managers myManager = new Managers();
    Gson gson = new Gson();

    JSONObject jo = new JSONObject();
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("ADMIN", "password");
    provider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
    HttpGet get = new HttpGet("http://localhost:8080/MainServerREST/api/managers/username/" + userName);

    HttpResponse response = client.execute(get);
    System.out.println("RESPONSE IS: " + response);
    JSONArray jsonArray = new JSONArray(
            IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8")));
    System.out.println(jsonArray);
    for (int i = 0; i < jsonArray.length(); i++) {
        jo = (JSONObject) jsonArray.getJSONObject(i);
        manager = gson.fromJson(jo.toString(), Managers.class);
        if (manager.getManUsername().equals(userName)) {
            password = manager.getManPassword();
        }/*from w w w . j a v  a 2s. c o  m*/

        System.out.println(password);

    }
    return password;
}

From source file:com.aurel.track.license.LicenseHelperBL.java

public static String sendPOSTReq(List<BasicNameValuePair> params, String licenseProviderAddress) {
    String responseStr = "";
    try {/*from   www .  ja v  a 2s . c o m*/
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(licenseProviderAddress);
        // Request parameters and other properties.
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        //Execute and get the response.
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            responseStr = StringArrayParameterUtils.getStringFromInputStream(instream);
            try {
                // do something useful
            } finally {
                instream.close();
            }
        }

    } catch (Exception ex) {
        LOGGER.error(ExceptionUtils.getStackTrace(ex));
    }
    int idx = responseStr.indexOf("}<!DOCTYPE HTML", 0);
    if (idx > 0) {
        responseStr = responseStr.substring(0, idx + 1);
    }
    return responseStr;
}

From source file:org.wso2.dss.integration.test.odata.ODataTestUtils.java

public static int sendDELETE(String endpoint, Map<String, String> headers) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpDelete httpDelete = new HttpDelete(endpoint);
    for (String headerType : headers.keySet()) {
        httpDelete.setHeader(headerType, headers.get(headerType));
    }/*w ww .ja v a  2s. com*/
    HttpResponse httpResponse = httpClient.execute(httpDelete);
    return httpResponse.getStatusLine().getStatusCode();
}

From source file:org.wso2.dss.integration.test.odata.ODataTestUtils.java

public static Object[] sendGET(String endpoint, Map<String, String> headers) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(endpoint);
    for (String headerType : headers.keySet()) {
        httpGet.setHeader(headerType, headers.get(headerType));
    }//from  w  w w .  ja v a  2  s  .co m
    HttpResponse httpResponse = httpClient.execute(httpGet);
    if (httpResponse.getEntity() != null) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(httpResponse.getEntity().getContent()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = reader.readLine()) != null) {
            response.append(inputLine);
        }
        reader.close();
        return new Object[] { httpResponse.getStatusLine().getStatusCode(), response.toString() };
    } else {
        return new Object[] { httpResponse.getStatusLine().getStatusCode() };
    }
}

From source file:com.seer.datacruncher.load.LoadRunner.java

private static HttpResponse postRequest(HttpClient client, String action) {
    HttpResponse response = null;//from   ww w.j a  v a  2s.  c  om
    try {
        HttpPost httpPostDatabase = new HttpPost(properties.getProperty("url") + "/" + action);
        System.out.println("Requesting: " + httpPostDatabase.getURI());
        response = client.execute(httpPostDatabase);
        printResponse(response);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}