Example usage for org.apache.http.client.methods HttpGet HttpGet

List of usage examples for org.apache.http.client.methods HttpGet HttpGet

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpGet HttpGet.

Prototype

public HttpGet(final String uri) 

Source Link

Usage

From source file:org.codehaus.mojo.license.utils.HttpRequester.java

/**
 * this method will send a simple GET-request to the given destination and will return the result as a
 * string/*from   ww  w. java2 s . c om*/
 * 
 * @param url the resource destination that is expected to contain pure text
 * @return the string representation of the resource at the given URL
 */
public static String getFromUrl(String url) throws MojoExecutionException {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet get = new HttpGet(url);
    CloseableHttpResponse response = null;

    String result = null;
    try {
        response = httpClient.execute(get);
        result = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
    } catch (ClientProtocolException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    }
    return result;
}

From source file:Main.java

public static HttpEntity getEntity(String uri, ArrayList<BasicNameValuePair> params, int method)
        throws ClientProtocolException, IOException {
    mClient = new DefaultHttpClient();
    HttpUriRequest request = null;//from w  ww .j ava2 s .c o m
    switch (method) {
    case METHOD_GET:
        StringBuilder sb = new StringBuilder(uri);
        if (params != null && !params.isEmpty()) {
            sb.append("?");
            for (BasicNameValuePair param : params) {
                sb.append(param.getName()).append("=").append(URLEncoder.encode(param.getValue(), "UTF_8"))
                        .append("&");
            }
            sb.deleteCharAt(sb.length() - 1);
        }
        request = new HttpGet(sb.toString());
        break;
    case METHOD_POST:
        HttpPost post = new HttpPost(uri);
        if (params != null && !params.isEmpty()) {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF_8");
            post.setEntity(entity);
        }
        request = post;
        break;
    }
    mClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
    mClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
    HttpResponse response = mClient.execute(request);
    System.err.println(response.getStatusLine().toString());
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        return response.getEntity();
    }
    return null;
}

From source file:com.zhch.example.commons.http.v4_5.ClientExecuteProxy.java

public static void proxyExample() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from  w  ww . java2  s .  c  o m
        HttpHost proxy = new HttpHost("24.157.37.61", 8080, "http");

        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        HttpGet request = new HttpGet("http://www.ip.cn");
        request.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1");

        request.setConfig(config);

        System.out.println(
                "Executing request " + request.getRequestLine() + " to " + request.getURI() + " via " + proxy);

        CloseableHttpResponse response = httpclient.execute(request);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity(), "utf8"));
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}