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:net.liuxuan.Tools.signup.SignupQjvpn.java

public void getLoginForm() throws IOException {

    HttpGet httpget = new HttpGet("http://www.qjvpn.com/user/login.php");
    CloseableHttpResponse response1 = httpclient.execute(httpget);
    try {//w ww. j a v  a 2  s. c  o m
        HttpEntity entity = response1.getEntity();
        //?once
        String content = EntityUtils.toString(entity);
        //                System.out.println(content);
        System.out.println("--------------");
        System.out.println("--------------");
        Document doc = Jsoup.parse(content);
        //                Elements inputs = doc.select("input[type=text]");
        Elements inputs = doc.select("input[type=hidden]");
        for (int i = 0; i < inputs.size(); i++) {
            Element element = inputs.get(i);
            params.add(new BasicNameValuePair(element.attr("name"), element.attr("value")));
            //                    params.put(element.attr("name"), element.attr("value"));
            System.out.println(element.toString());
            System.out.println(element.attr("name"));
            System.out.println(element.attr("value"));

        }

        System.out.println("--------------");
        System.out.println("--------------");

        System.out.println("--------------");
        System.out.println("--------------");
        System.out.println("Login form get: " + response1.getStatusLine());
        EntityUtils.consume(entity);

        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = cookieStore.getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
    } finally {
        response1.close();
    }

    //            HttpUriRequest login = RequestBuilder.post()
    //                    .setUri(new URI("http://v2ex.com/signin"))
    //                    .addParameter("u", "mosliu")
    //                    .addParameter("p", "mosesmoses")
    //                    .build();
    //            CloseableHttpResponse response2 = httpclient.execute(login);
    //            try {
    //                HttpEntity entity = response2.getEntity();
    //
    //                System.out.println("Login form get: " + response2.getStatusLine());
    //                
    //                EntityUtils.consume(entity);
    //
    //                System.out.println("Post logon cookies:");
    //                List<Cookie> cookies = cookieStore.getCookies();
    //                if (cookies.isEmpty()) {
    //                    System.out.println("None");
    //                } else {
    //                    for (int i = 0; i < cookies.size(); i++) {
    //                        System.out.println("- " + cookies.get(i).toString());
    //                    }
    //                }
    //                
    //                
    //                
    //            } finally {
    //                response2.close();
    //            }
    //            
    //            
    //            httpget = new HttpGet("http://v2ex.com/signin");
    //            response1 = httpclient.execute(httpget);
    //            try {
    //                HttpEntity entity = response1.getEntity();
    //                String content = EntityUtils.toString(entity);
    //                System.out.println("-----------------content---------------------");
    //                System.out.println(content);
    //                
    //                EntityUtils.consume(entity);
    //            } finally {
    //                response1.close();
    //            }
    //            
    //            
}

From source file:org.solovyev.android.messenger.realms.vk.auth.VkOauthHttpTransaction.java

@Override
public JsonAuthResult getResponse(@Nonnull HttpResponse response) {
    boolean ok = response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
    if (!ok) {/* w w w . jav  a  2 s.c o m*/
        throw new HttpRuntimeIoException(new IOException());
    }

    final String json;
    try {
        json = EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        throw new HttpRuntimeIoException(e);
    }

    try {
        return JsonAuthResult.fromJson(json);
    } catch (IllegalJsonException e) {
        throw VkResponseErrorException.newInstance(json, this);
    }
}

From source file:com.cloud.utils.rest.HttpUriRequestPayloadMatcher.java

@Override
protected String featureValueOf(final HttpUriRequest actual) {
    String payload = "";
    if (actual instanceof HttpEntityEnclosingRequest) {
        try {// www .j  a va  2 s .  co m
            payload = EntityUtils.toString(((HttpEntityEnclosingRequest) actual).getEntity());
        } catch (final ParseException e) {
            throw new IllegalArgumentException("Couldn't read request's entity payload.", e);
        } catch (final IOException e) {
            throw new IllegalArgumentException("Couldn't read request's entity payload.", e);
        }
    }
    return payload;
}

From source file:com.iluwatar.aggregator.microservices.ProductInventoryClientImpl.java

@Override
public int getProductInventories() {
    String response = "0";
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpGet = new HttpGet("http://localhost:51516/inventories");
        try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
            response = EntityUtils.toString(httpResponse.getEntity());
        }/* w  w  w .j av a  2 s  . co  m*/
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Integer.parseInt(response);
}

From source file:main.java.com.skillselector.client.root.Neo4JRequest.java

public Map<String, HashMap<String, String>> getContent(String postAddress, String Payload) {
    Map<String, HashMap<String, String>> content = new HashMap<String, HashMap<String, String>>();
    HttpClient client = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(postAddress);
    httppost.setHeader("Content-Type", "text/x-gwt-rpc; charset=UTF-8");
    StringEntity se;/*from   www  .j  a  va 2s.  com*/
    try {
        se = new StringEntity(Payload);
        httppost.setEntity(se);
        HttpResponse response = client.execute(httppost);
        String json = EntityUtils.toString(response.getEntity());
        JSONObject temp1 = new JSONObject(json);
        JSONArray array = (JSONArray) temp1.get("data");
        //  System.out.println(json);
        for (int i = 0; i < array.length(); i++) {
            JSONArray subarray = (JSONArray) array.get(i);
            if (content.containsKey(subarray.get(0))) {
                HashMap<String, String> submap = content.get(subarray.get(0));
                submap.put(subarray.getString(1), subarray.getString(2));

            } else {
                HashMap<String, String> submap = new HashMap<String, String>();
                submap.put(subarray.getString(1), subarray.getString(2));
                content.put((String) subarray.get(0), submap);
            }
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(Neo4JRequest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException e) {
        Logger.getLogger(Neo4JRequest.class.getName()).log(Level.SEVERE, null, e);
    }

    return content;
}

From source file:org.flakor.androidtool.utils.HttpUtil.java

public static String queryStringForPost(HttpPost request) {
    String result = null;/* ww w . j  a  v  a 2  s .c om*/
    try {
        // ?
        HttpResponse response = HttpUtil.getHttpResponse(request);
        // ??
        if (response.getStatusLine().getStatusCode() == 200) {
            // ?
            result = EntityUtils.toString(response.getEntity());
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        result = "?";
    } catch (IOException e) {
        e.printStackTrace();
        result = "?";
    } finally {
        return result;
    }
}

From source file:com.iluwatar.aggregator.microservices.ProductInformationClientImpl.java

@Override
public String getProductTitle() {
    String response = null;//from w  w  w . j  a v  a  2  s .  co m
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpGet httpGet = new HttpGet("http://localhost:51515/information");
        try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
            response = EntityUtils.toString(httpResponse.getEntity());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

From source file:es.tid.fiware.rss.oauth.service.ResponseHandler.java

/**
 * handle response.//from  ww  w  . j a  va  2s. c  om
 * 
 * @throws IOException
 */
@Override
public HttpResponse handleResponse(final HttpResponse response) throws IOException {
    ResponseHandler.log.debug("into handleResponse method");
    status = response.getStatusLine().getStatusCode();
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        content = true;
        ResponseHandler.log.debug("----------------------------------------");
        ResponseHandler.log.debug("Response content:");
        responseContent = EntityUtils.toString(entity);
        ResponseHandler.log.debug(responseContent);
    } else {
        content = false;
    }
    return response;
}

From source file:org.bonitasoft.engine.http.BonitaResponseHandler.java

/**
 * Returns the response body as a String if the response was successful (a
 * 2xx status code). If no response body exists, this returns null. If the
 * response was unsuccessful (&gt;= 300 status code), throws an {@link org.apache.http.client.HttpResponseException}.
 *///ww  w .j  a va2 s.co  m
@Override
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
    final HttpEntity entity = response.getEntity();
    final StatusLine statusLine = response.getStatusLine();

    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }

    return entity == null ? null : EntityUtils.toString(entity);
}

From source file:uk.ac.bbsrc.tgac.miso.integration.util.IntegrationUtils.java

public static String makePostRequest(String host, int port, String query) throws IntegrationException {
    if (query == null || query.isEmpty()) {
        throw new IntegrationException("Query must be populated when calling makePostRequest.");
    }/*from   ww w. jav a  2s .c om*/

    String rtn = null;
    HttpClient httpclient = HttpClientBuilder.create().build();
    String url = "http://" + host + ":" + port + "/miso/";

    try {
        HttpPost httpPost = new HttpPost(url);
        // Request parameters and other properties.
        httpPost.setHeader("content-type", "application/x-www-form-urlencoded");
        System.out.println(query);
        httpPost.setEntity(new StringEntity(query));

        // Execute and get the response.
        HttpResponse response = httpclient.execute(httpPost);
        rtn = EntityUtils.toString(response.getEntity());
        if (rtn.charAt(0) == '"' && rtn.charAt(rtn.length() - 1) == '"') {
            System.out.println("Removing quotes");
            rtn = rtn.substring(1, rtn.length() - 1);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new IntegrationException("Cannot connect to " + url + " Cause: " + ex.getMessage());

    }
    return rtn;

}