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:Main.java

public static String getNetPostStirng(String url) {
    HttpClient httpClient = new DefaultHttpClient();
    String data = null;//from ww  w. j  av  a2s.c o m
    HttpPost httpPost = new HttpPost(url);
    try {
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            data = EntityUtils.toString(response.getEntity(), "utf-8");
            //            EntityUtils.toByteArray(response.getEntity());
            //            Log.i("ll", "--->"+data);
        } else {
            return null;
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return data;
}

From source file:org.jboss.additional.testsuite.jdkall.present.clustering.cluster.web.ExternalizerTestCase.java

private static void assertValue(HttpClient client, URI uri, int value) throws IOException {
    HttpResponse response = client.execute(new HttpGet(uri));
    try {//from   ww w .j  av  a  2 s. co m
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
        Assert.assertEquals(value,
                Integer.parseInt(response.getFirstHeader(CounterServlet.COUNT_HEADER).getValue()));
    } finally {
        HttpClientUtils.closeQuietly(response);
    }
}

From source file:it.unicaradio.android.utils.NetworkUtils.java

public static byte[] httpPost(String url, byte[] postData, String contentType)
        throws ClientProtocolException, IOException, HttpException {
    HttpParams httpParams = new BasicHttpParams();

    HttpPost post = new HttpPost(url);
    post.setEntity(new ByteArrayEntity(postData));
    post.addHeader("Content-Type", contentType);

    HttpClient client = new DefaultHttpClient(httpParams);
    HttpResponse response = client.execute(post);

    HttpEntity httpEntity = response.getEntity();
    if (httpEntity == null) {
        throw new HttpException("No answer from server.");
    }//from  w w  w .  ja v a  2 s .  co m

    return EntityUtils.toByteArray(httpEntity);
}

From source file:it410.gmu.edu.OrderingServiceClient.java

public static void getCustomersJSON() throws Exception {

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://localhost:8080/BookstoreRestService/generic/getOrdersJSON");
    HttpResponse response = client.execute(request);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String line = "";
    StringBuilder sb = new StringBuilder();

    while ((line = rd.readLine()) != null) {
        //System.out.println(line);
        sb.append(line);/*w w  w.j  a  v  a  2 s .c  om*/
    }
    System.out.println("Customers JSON = " + sb.toString());

    Gson gson = new Gson();
    Customers customers = gson.fromJson(new StringReader(sb.toString()), Customers.class);

    System.out.println(" JSON Customer List size = " + customers.getCustomersJSON().size());
}

From source file:org.test.LookupSVNUsers.java

private static String extractFullName(String userName) throws IOException {

    HttpClient client = new DefaultHttpClient();

    HttpResponse response = client.execute(new HttpGet("https://sourceforge.net/users/" + userName));

    HttpEntity entity = response.getEntity();

    List<String> content = IOUtils.readLines(entity.getContent(), "UTF-8");

    int i = 0;//from w w w .j  a  v a 2s.  co m

    for (String cLine : content) {

        String trimmed = cLine.trim();

        if (cLine.contains("/users/" + userName)) {

            String[] parts = trimmed.split("</a> ");

            if (parts.length > 1) {
                String name = parts[1].replaceAll("</h1>", "");

                return name;
            }

        }

    }

    return null;
}

From source file:org.hobbit.spatiotemporalbenchmark.transformations.value.AddressFromNominatim.java

public static String getAddress(double latitude, double longitude) {
    String strAddress = "";
    String point = latitude + "," + longitude;

    String url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" + latitude + "&lon=" + longitude
            + "&addressdetails=1&accept-language=en";

    HttpPost httppost = new HttpPost(url);
    HttpClient httpclient = new DefaultHttpClient();
    String result = "";
    try {/*  ww  w  .java 2  s .c o  m*/
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntityGet = response.getEntity();
        if (resEntityGet != null) {
            result = EntityUtils.toString(resEntityGet);
        }
    } catch (Exception e) {
        System.out.println("Exception:" + e);
    }
    try {
        if (result != null) {
            JSONObject json = new JSONObject(result);
            strAddress = json.get("display_name").toString();
        }
    } catch (Exception e) {
    }
    try {
        if (!strAddress.equals("")) {
            pointAddressMap.put(point, strAddress);
            FileUtils.writeStringToFile(f, point + "=" + strAddress + "\n", true);
        }
    } catch (IOException ex) {
        Logger.getLogger(CoordinatesToAddress.class.getName()).log(Level.SEVERE, null, ex);
    }
    return strAddress;
}

From source file:com.alta189.cyborg.factoids.util.HTTPUtil.java

public static String readRandomLine(String url) {
    HttpGet request = new HttpGet(url);
    HttpClient httpClient = new DefaultHttpClient();
    try {//from  ww  w .ja v a  2  s.  c  om
        HttpResponse response = httpClient.execute(request);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        List<String> lines = new ArrayList<String>();
        String line = "";
        while ((line = rd.readLine()) != null) {
            lines.add(line);
        }

        if (lines.size() > 1) {
            int i = randomNumber(0, lines.size() + 1);
            return lines.get(i);
        } else if (lines.size() == 1) {
            return lines.get(0);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:language_engine.HttpUtils.java

public static String doHttpGet(String url) {
    try {// ww  w.ja  v  a  2 s.c o m
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse resp = httpClient.execute(httpGet);

        return StreamUtil.readText(resp.getEntity().getContent(), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.liferay.mobile.android.util.PortalVersionUtil.java

protected static int getBuilderNumberHeader(String url) throws IOException {
    HttpClient client = new DefaultHttpClient();
    HttpHead head = new HttpHead(url);
    HttpResponse response = client.execute(head);

    Header portalHeader = response.getFirstHeader("Liferay-Portal");

    if (portalHeader == null) {
        return PortalVersion.UNKNOWN;
    }// www .j a  v a2 s.  c  om

    String portalField = portalHeader.getValue();

    int indexOfBuild = portalField.indexOf("Build");

    if (indexOfBuild == -1) {
        return PortalVersion.UNKNOWN;
    } else {
        String buildNumber = portalField.substring(indexOfBuild + 6, indexOfBuild + 10);

        return Integer.valueOf(buildNumber);
    }
}

From source file:org.ow2.bonita.facade.rest.apachehttpclient.ApacheHttpClientUtil.java

public static HttpResponse executeHttpConnection(String uri, List<NameValuePair> formParams, String options,
        String username, String password) throws URISyntaxException, IOException, IOException {
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
    String serverAddress = HttpRESTUtil.getRESTServerAddress();
    HttpPost post = new HttpPost(serverAddress + uri);
    post.setEntity(entity);/*  w  w  w. ja va 2s . com*/
    HttpClient client = ApacheHttpClientUtil.getHttpClient(serverAddress, username, password);
    HttpResponse httpresponse = client.execute(post);
    return httpresponse;
}