Example usage for org.apache.http.util EntityUtils toString

List of usage examples for org.apache.http.util EntityUtils toString

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils toString.

Prototype

public static String toString(HttpEntity httpEntity) throws IOException, ParseException 

Source Link

Usage

From source file:org.fcrepo.integration.api.FedoraIdentifiersIT.java

@Test
public void testGetNextHasAPid() throws IOException {
    final HttpPost method = new HttpPost(serverAddress + "nextPID?numPids=1");
    method.addHeader("Accepts", "text/xml");
    final HttpResponse response = client.execute(method);
    logger.debug("Executed testGetNextHasAPid()");
    final String content = EntityUtils.toString(response.getEntity());
    logger.debug("Only to find:\n" + content);
    assertTrue("Didn't find a single dang PID!", compile("<pid>.*?</pid>", DOTALL).matcher(content).find());
}

From source file:ca.sqlpower.http.HttpResponseHandler.java

public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    StatusLine statusLine = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    String entityString = EntityUtils.toString(entity);
    // Attempt to get the Server stacktrace and display that
    if (statusLine.getStatusCode() == 500) {
        throw new HttpResponseException(statusLine.getStatusCode(), entityString);
    } else if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }/*from  w  ww . j ava  2 s.c o m*/

    return entity == null ? null : entityString;
}

From source file:org.kitodo.data.elasticsearch.index.type.FilterTypeTest.java

@Test
public void shouldCreateDocument() throws Exception {
    FilterType propertyType = new FilterType();
    JSONParser parser = new JSONParser();

    Filter property = prepareData().get(0);
    HttpEntity document = propertyType.createDocument(property);
    JSONObject actual = (JSONObject) parser.parse(EntityUtils.toString(document));
    JSONObject expected = (JSONObject) parser.parse("{\"value\":\"\\\"id:1\\\"\",\"user\":1}");
    assertEquals("Filter JSONObject doesn't match to given JSONObject!", expected, actual);

    property = prepareData().get(1);/*from  w w  w. ja  v  a2s  .com*/
    document = propertyType.createDocument(property);
    actual = (JSONObject) parser.parse(EntityUtils.toString(document));
    expected = (JSONObject) parser.parse("{\"value\":\"\\\"id:2\\\"\",\"user\":1}");
    assertEquals("Filter JSONObject doesn't match to given JSONObject!", expected, actual);
}

From source file:org.elasticsearch.shell.command.HttpCommandResponse.java

private String extractContent(HttpEntity entity) {
    if (entity == null) {
        return "";
    }//from   www.j  ava 2  s .co  m

    try {
        return EntityUtils.toString(entity);
    } catch (IOException e) {
        throw new RuntimeException("Error while extracting http response content", e);
    }
}

From source file:synapticloop.b2.request.B2CancelLargeFileRequest.java

public B2FileResponse getResponse() throws B2ApiException, IOException {
    return new B2FileResponse(EntityUtils.toString(executePost().getEntity()));
}

From source file:self.philbrown.javaQuery.JSONResponseHandler.java

@Override
public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() >= 300) {
        Log.e("javaQuery",
                "HTTP Response Error " + statusLine.getStatusCode() + ":" + statusLine.getReasonPhrase());
        return null;
    }//from w  ww .  ja  v a 2 s  . co  m

    HttpEntity entity = response.getEntity();
    if (entity == null)
        return null;

    String json = null;
    try {
        json = EntityUtils.toString(entity);
        if (json.startsWith("{")) {
            return new JSONObject(json);
        } else {
            return new JSONArray(json);
        }

    } catch (ParseException e) {
        throw e;
    } catch (JSONException e) {
        throw new IOException("Received malformed JSON");

    }
}

From source file:pamela.client2.PamelaWebservice.java

protected List<String> getMacs() {
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
    HttpConnectionParams.setSoTimeout(httpParams, 10000);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);

    String json = "";

    try {//from  w w w  .ja va 2  s  .  c  om
        URI uri = new URI(url);
        HttpGet httpGet = new HttpGet(uri);
        HttpResponse response = httpClient.execute(httpGet);
        json = EntityUtils.toString(response.getEntity());
    } catch (Exception e) {
        e.printStackTrace();
    }

    ArrayList<String> macs = new ArrayList<String>();

    int i = 0;

    for (String part : json.split("\"")) {
        i++;

        // These will be the macs.
        // Assuming no lamefag put's an " in his name :)
        if (i % 2 == 0) {
            macs.add(part);
        }
    }

    return (List<String>) macs;
}

From source file:com.project.utilities.Utilities.java

/**
 * Sends post request to sepcified URL/*w ww  .  ja v a2s .c o m*/
 * @param valuePairs
 * @param postUrl
 * @return
 */
public static String postRequest(final List<BasicNameValuePair> valuePairs, final String postUrl) {
    String responseStr = null;

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    HttpClient httpclient = new DefaultHttpClient();
    Log.e("POST_REQUEST", "ACTION LOGIN");
    HttpPost httppost = new HttpPost(postUrl);

    try {
        // Add your data
        List<NameValuePair> postFields = new ArrayList<NameValuePair>(2);
        for (BasicNameValuePair nameValuePair : valuePairs) {
            postFields.add(nameValuePair);
        }
        httppost.setEntity(new UrlEncodedFormEntity(postFields));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        responseStr = EntityUtils.toString(response.getEntity());
        Log.e("POST_REQUEST", "RESPONSE_CODE: " + response.getStatusLine().getStatusCode() + "");
    } catch (ClientProtocolException e) {
        Log.e("ClientProtocolException", e.getMessage().toString());
    } catch (IOException e) {
        Log.e("IOException", e.getMessage().toString());
    }

    return responseStr;
}

From source file:com.android.internal.util.weather.HttpRetriever.java

public String retrieve(String url) {
    HttpGet get = new HttpGet(url);
    try {/*from  w ww . ja v  a2 s  .c  o  m*/
        HttpResponse getResponse = client.execute(get);
        HttpEntity getResponseEntity = getResponse.getEntity();
        if (getResponseEntity != null) {
            String response = EntityUtils.toString(getResponseEntity);
            return response;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.whatsthatlight.teamcity.hipchat.test.SimpleServerTest.java

@Test
public void testTestServer() throws Exception {
    // Test parameters
    String expectedResponse = "<h1>Hello World</h1>";
    int expectedStatusCode = HttpServletResponse.SC_OK;
    int port = 8080;
    URI uri = new URI(String.format("http://localhost:%s/", port));

    // Setup//from  www. j  a va 2s  . co  m
    SimpleServer server = new SimpleServer(port, new SimpleHandler(expectedResponse, expectedStatusCode));
    server.start();

    // Make request
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet getRequest = new HttpGet(uri.toString());
    HttpResponse getResponse = client.execute(getRequest);
    int actualStatusCode = getResponse.getStatusLine().getStatusCode();
    String actualResponse = EntityUtils.toString(getResponse.getEntity());

    // Clean up
    server.stop();

    // Test
    assertEquals(expectedStatusCode, actualStatusCode);
    assertEquals(expectedResponse, actualResponse);
}