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:com.phonegap.HttpHandler.java

private HttpEntity getHttpEntity(String url)
/**//  w  ww  .  ja  va2  s  .c  o  m
 * get the http entity at a given url
 */
{
    HttpEntity entity = null;
    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        entity = response.getEntity();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return entity;
}

From source file:net.nym.library.cookie.CookieTest.java

private String getRequest(Context context, String urlString, Map<String, Object> params) throws IOException {
    StringBuffer param = new StringBuffer();
    int i = 0;//w  w  w .j  av  a2  s  .  c o  m
    for (String key : params.keySet()) {
        if (i == 0)
            param.append("?");
        else
            param.append("&");
        param.append(key).append("=").append(params.get(key));
        i++;
    }

    Log.i(urlString + param.toString());
    //URL?
    HttpGet getMethod = new HttpGet(urlString + param.toString());

    DefaultHttpClient client = new DefaultHttpClient();

    client.setCookieStore(new PersistentCookieStore(context));
    HttpResponse response = client.execute(getMethod);
    if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
        String result = EntityUtils.toString(response.getEntity(), "utf-8");
        return result;
    }
    return null;
}

From source file:com.github.util.HttpHandler.java

private String executeRequest(HttpUriRequest request) {
    DefaultHttpClient client = new DefaultHttpClient();
    try {/* www  . j  a va 2 s .  c o  m*/
        HttpResponse execute = client.execute(request);
        Header firstHeader = execute.getFirstHeader("Content-Encoding");
        HttpEntity response = execute.getEntity();
        InputStream inputStream = response.getContent();
        if (firstHeader != null && firstHeader.getValue() != null
                && firstHeader.getValue().equalsIgnoreCase("gzip")) {
            inputStream = new GZIPInputStream(inputStream);
        }
        return streamToString(inputStream);
    } catch (IOException e) {
        return null;
    }
}

From source file:org.wso2.carbon.esb.passthru.transport.test.CookieHeaderExpiresHavingCommaTestCase.java

@Test(groups = "wso2.esb", description = "Test SetCookieHeader With Expires having a comma")
public void testSetCookieHeaderWithExpires() throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(getApiInvocationURL("cookieHeaderTestAPI"));
    HttpResponse httpResponse = httpclient.execute(httpGet);
    Assert.assertEquals(httpResponse.getStatusLine().getStatusCode(), 200,
            "Request failed. Response received is : " + httpResponse.toString());
}

From source file:org.wso2.carbon.esb.rest.test.api.APIHeadMethod.java

@Test(groups = "wso2.esb", description = "API HTTP HEAD Method")
public void apiHTTPHeadMethodTest() throws Exception {
    String restURL = "http://localhost:8480/headTest";
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpHead httpHead = new HttpHead(restURL);
    HttpResponse response = httpclient.execute(httpHead);

    Assert.assertTrue(stringExistsInLog("API_HIT"));

    // http head method should return a 200 OK
    assertTrue(response.getStatusLine().getStatusCode() == 200);
    // it should not contain a message body
    assertTrue(response.getEntity() == null);
}

From source file:com.nullwire.trace.ExceptionHandler.java

/**
 * Look into the files folder to see if there are any "*.stacktrace" files.
 * If any are present, submit them to the trace server.
 *//*from   w  w  w . jav a2 s. com*/
public static void submitStackTraces() {
    try {
        Log.d(TAG, "Looking for exceptions in: " + G.FILES_PATH);
        //TODO append logs to related stacktraces

        //collecting log records
        String[] logs = searchForLogs();
        String logRecords = null;
        StringBuilder logcontents = new StringBuilder();
        if (logs != null && logs.length > 0) {
            Log.d(TAG, "Found " + logs.length + " log(s)");
            for (int i = 0; i < logs.length; i++) {
                String filePath = G.FILES_PATH + "/" + logs[i];
                logcontents.append("\n======== LOGS ===========");
                BufferedReader input = new BufferedReader(new FileReader(filePath));
                String line = null;
                while ((line = input.readLine()) != null) {
                    logcontents.append(line);
                }
                input.close();
            }
        }
        logRecords = logcontents.toString();

        //collecting and sending stacktrace
        String[] list = searchForStackTraces();
        if (list != null && list.length > 0) {
            Log.d(TAG, "Found " + list.length + " stacktrace(s)");
            for (int i = 0; i < list.length; i++) {
                String filePath = G.FILES_PATH + "/" + list[i];
                // Extract the version from the filename: "packagename-version-...."
                String version = list[i].split("-")[0];
                Log.d(TAG, "Stacktrace in file '" + filePath + "' belongs to version " + version);
                // Read contents of stacktrace
                StringBuilder contents = new StringBuilder();
                BufferedReader input = new BufferedReader(new FileReader(filePath));
                String line = null;
                String androidVersion = null;
                String phoneModel = null;
                while ((line = input.readLine()) != null) {
                    if (androidVersion == null) {
                        androidVersion = line;
                        continue;
                    } else if (phoneModel == null) {
                        phoneModel = line;
                        continue;
                    }
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
                input.close();
                String stacktrace;
                stacktrace = contents.toString();

                Log.d(TAG, "Transmitting stack trace: " + stacktrace);
                // Transmit stack trace with POST request
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(G.URL);
                List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                nvps.add(new BasicNameValuePair("package_name", G.APP_PACKAGE));
                nvps.add(new BasicNameValuePair("package_version", version));
                nvps.add(new BasicNameValuePair("phone_model", phoneModel));
                nvps.add(new BasicNameValuePair("android_version", androidVersion));
                nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
                nvps.add(new BasicNameValuePair("log", logRecords));
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
                // We don't care about the response, so we just hope it went well and on with it
                httpClient.execute(httpPost);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        deleteStackTrace();
    }
}

From source file:de.doering.dwca.invasivespecies.ChecklistBuilder.java

private void parsePage() throws IOException {
    // get webapge
    log.info("Downloading latest invasives webpage from {}", WEBAPGE);

    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(WEBAPGE);

    // execute//from  ww  w .  j a va2 s. c  o  m
    HttpResponse response = client.execute(get);
    HttpEntity entity = response.getEntity();
    // parse page
    final Parser parser = new Parser();
    InvasivespeciesHandler handler = new InvasivespeciesHandler(writer, LINK_BASE);
    try {
        parser.setContentHandler(handler);
        parser.setFeature(Parser.namespacesFeature, false);
        Reader reader = new InputStreamReader(entity.getContent(), ENCODING);
        parser.parse(new InputSource(reader));
    } catch (Exception e) {
        log.error("Cannot process page", e);
    }
}

From source file:me.kaidul.uhunt.JSONDownloader.java

public InputStreamReader getJSONStringFromUrl(String url) {

    try {//from  w w  w  .  j a  va 2  s  .co  m
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {

    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    }

    try {
        return new InputStreamReader(is, "UTF-8");
    } catch (Exception e) {
        if (CommonUtils.isDebuggable) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        return null;
    }
}

From source file:com.novel.lightnovel.Utils.DrawableManager.java

private InputStream fetch(String urlString) throws IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(urlString);
    HttpResponse response = httpClient.execute(request);
    return response.getEntity().getContent();
}

From source file:org.jboss.as.test.integration.jaxrs.validator.HibernateValidatorProviderTestCase.java

@Test
public void testHibernateValidatorProvider() throws Exception {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(url + "myjaxrs/validate/5");
    HttpResponse result = client.execute(get);
    Assert.assertEquals(200, result.getStatusLine().getStatusCode());
    Assert.assertEquals("ValidatorModel{id=5}", EntityUtils.toString(result.getEntity()));

    get = new HttpGet(url + "myjaxrs/validate/3");
    result = client.execute(get);//from   ww w. ja v a2s .  com
    Assert.assertEquals(500, result.getStatusLine().getStatusCode());
}