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

public static String getJSONResponseFromURL(String url, Hashtable<String, String> httpGetParams) {
    String json_string = "";
    List<NameValuePair> nvps = buildNameValuePair(httpGetParams);
    url = buildGetUrl(nvps, url);// ww w  . j  av a  2s. c om
    System.out.println("URL==>" + url);
    InputStream is = null;
    try {
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = getThreadSafeClient().execute(httpget);

        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
        String line = null;
        while ((line = reader.readLine()) != null) {
            json_string = json_string + line;
        }
        response.getEntity().consumeContent();
        System.out.println("Json Response==>" + json_string);
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection" + e.toString());
        return null;
    }
    return json_string;
}

From source file:functional.PathTraversingTest.java

@Test
public void testTraversalDenied() throws Exception {
    HttpGet httpget = new HttpGet("http://localhost:8080/../../");

    CloseableHttpResponse execute = httpclient.execute(httpget);
    execute.close();//  w  ww. ja va2  s  .  c  o  m
    assertEquals(403, execute.getStatusLine().getStatusCode());
}

From source file:org.opencastproject.remotetest.server.resource.CaptureResources.java

public static HttpResponse startCaptureGet(TrustedHttpClient client) {
    return client.execute(new HttpGet(getServiceUrl() + "startCapture"));
}

From source file:es.deustotech.piramide.utils.net.RestClient.java

public static HttpEntity connect(String url, HttpEntity httpEntity) {
    final DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = new BasicHttpParams();
    params.setParameter("http.protocol.content-charset", "UTF-8");
    httpClient.setParams(params);//from   ww w  . j ava  2s  . c o  m
    final HttpGet httpGet = new HttpGet(url); //request object
    HttpResponse response = null;

    try {
        response = httpClient.execute(httpGet);
    } catch (ClientProtocolException cpe) {
        Log.d(Constants.TAG, cpe.getMessage());
    } catch (IOException ioe) {
        Log.d(Constants.TAG, ioe.getMessage());
    }
    return httpEntity = response.getEntity();
}

From source file:br.unicamp.busfinder.ServerOperations.java

public static JSONArray getJSON(String site) {

    Log.d("Executing REquest", site);

    StringBuilder builder = new StringBuilder();

    HttpGet get = new HttpGet(site);

    HttpClient client = new DefaultHttpClient();

    HttpResponse response;/*w  w  w .  j ava  2s. c o  m*/
    try {
        response = client.execute(get);

        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("ERRRO", "Failed to download file");
        }

        Log.d("RESP:", builder.toString());

        return new JSONArray(builder.toString());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;

}

From source file:org.npr.api.HttpHelper.java

/**
 * A helper function to grab content from a URL.
 *
 * @param url URL of the item to download
 *
 * @return an input stream to the content. The caller is responsible for
 * closing the stream. Content will be null in the case of errors.
 * @throws java.io.IOException if an error occurs loading the url
 *//*from  w  ww .  jav  a2  s . c  o m*/
public static InputStream download(String url) throws IOException {
    InputStream data = null;
    Log.d(LOG_TAG, "Starting download: " + url);
    HttpClient http = new DefaultHttpClient();
    HttpGet method = new HttpGet(url);

    try {
        HttpResponse response = http.execute(method);
        data = response.getEntity().getContent();
    } catch (IllegalStateException e) {
        Log.e(LOG_TAG, "error downloading", e);
    }
    Log.d(LOG_TAG, "Download complete");
    return data;
}

From source file:com.github.rnewson.couchdb.lucene.couchdb.HttpUtils.java

public static final String get(final HttpClient httpClient, final String url) throws IOException {
    return execute(httpClient, new HttpGet(url));
}

From source file:org.keycloak.adapters.cloned.HttpAdapterUtils.java

public static MultivaluedHashMap<String, KeyInfo> downloadKeysFromSamlDescriptor(HttpClient client,
        String descriptorUrl) throws HttpClientAdapterException {
    try {/*from w ww  . j  av a  2s.c  om*/
        HttpGet httpRequest = new HttpGet(descriptorUrl);
        HttpResponse response = client.execute(httpRequest);
        int status = response.getStatusLine().getStatusCode();
        if (status != HttpStatus.SC_OK) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw new HttpClientAdapterException("Unexpected status = " + status);
        }

        HttpEntity entity = response.getEntity();
        if (entity == null) {
            throw new HttpClientAdapterException("There was no entity.");
        }

        MultivaluedHashMap<String, KeyInfo> res;
        try (InputStream is = entity.getContent()) {
            res = extractKeysFromSamlDescriptor(is);
        }

        EntityUtils.consumeQuietly(entity);

        return res;
    } catch (IOException | ParsingException e) {
        throw new HttpClientAdapterException("IO error", e);
    }
}

From source file:wuit.common.crawler.WebSit.Crawler.java

public static String doGetHttp(DSCrawlerUrl pageUrl) {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 12000);
    HttpConnectionParams.setSoTimeout(params, 9000);
    HttpClient httpclient = new DefaultHttpClient(params);
    String rs = "";
    try {/*from   w  ww . j a  v a2 s.c o m*/
        HttpGet httpget = new HttpGet(pageUrl.url);
        //            System.out.println("executing request " + pageUrl.url);
        HttpContext httpContext = new BasicHttpContext();
        //            httpget.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
        httpget.addHeader("User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)");

        HttpResponse response = httpclient.execute(httpget, httpContext);
        HttpUriRequest realRequest = (HttpUriRequest) httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
        HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        pageUrl.url = targetHost.toString() + realRequest.getURI();
        int resStatu = response.getStatusLine().getStatusCode();//? 
        if (resStatu == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                rs = EntityUtils.toString(entity, "iso-8859-1");
                String in_code = getEncoding(rs);
                String encode = getHtmlEncode(rs);
                if (encode.isEmpty()) {
                    httpclient.getConnectionManager().shutdown();
                    return "";
                } else {
                    if (!in_code.toLowerCase().equals("utf-8")
                            && !in_code.toLowerCase().equals(encode.toLowerCase())) {
                        if (!in_code.toLowerCase().equals("iso-8859-1"))
                            rs = new String(rs.getBytes("iso-8859-1"), in_code);
                        if (!encode.toLowerCase().equals(in_code.toLowerCase()))
                            rs = new String(rs.getBytes(in_code), encode);
                    }
                }
                try {
                } catch (RuntimeException ex) {
                    httpget.abort();
                    throw ex;
                } finally {
                    // Closing the input stream will trigger connection release
                    //                    try { instream.close(); } catch (Exception ignore) {}
                }
            }
        }
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
        return rs;
    }
}

From source file:com.nuance.expertassistant.HTTPConnection.java

public static JSONObject sendGet(String getURL) throws Exception {

    String url = getURL;/*  w ww .j  a  va 2 s  .  c o m*/

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);

    // add request header
    request.addHeader("User-Agent", USER_AGENT);

    System.out.println(" The URL is :" + url);

    HttpResponse response = client.execute(request);

    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuilder builder = new StringBuilder();
    for (String line = null; (line = rd.readLine()) != null;) {
        builder.append(line).append("\n");
    }

    System.out.print("JSON TEXT : " + builder.toString());

    String jsonText = builder.toString();

    if (jsonText.startsWith("[")) {
        jsonText = jsonText.substring(1, jsonText.length());
    }

    if (jsonText.endsWith("]")) {
        jsonText = jsonText.substring(0, jsonText.length() - 1);
    }

    JSONObject object = new JSONObject(jsonText);

    return object;

}