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:au.org.ala.bhl.service.WebServiceHelper.java

/**
 * Performs a GET on the specified URI, and expects that the output is well formed JSON. The output is parsed into a JsonNode for consumption
 * /*w  w  w .  java  2  s  .  c  o  m*/
 * @param uri
 * @return
 * @throws IOException
 */
public static JsonNode getJSON(String uri) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(uri);
    httpget.setHeader("Accept", "application/json");
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        @SuppressWarnings("unchecked")
        List<String> lines = IOUtils.readLines(instream, "utf-8");
        String text = StringUtils.join(lines, "\n");
        try {
            JsonNode root = new ObjectMapper().readValue(text, JsonNode.class);
            return root;
        } catch (Exception ex) {
            log("Error parsing results for request: %s\n%s\n", uri, text);
            ex.printStackTrace();
        }

    }
    return null;
}

From source file:com.unitedcoders.android.gpodroid.tools.Tools.java

public static String getImageUrlFromFeed(Context context, String url) {

    try {//w  ww.  j a va2 s . c om
        Document doc;
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);
        doc = builder.parse(response.getEntity().getContent());
        //            return doc;

        // get Image
        NodeList item = doc.getElementsByTagName("channel");
        Element el = (Element) item.item(0);

        String imageUrl;
        NodeList imagNode = el.getElementsByTagName("image");
        if (imagNode != null) {
            Element ima = (Element) imagNode.item(0);
            if (ima != null) {
                NodeList urlNode = ima.getElementsByTagName("url");
                if (urlNode == null || urlNode.getLength() < 1)
                    imageUrl = null;
                else
                    imageUrl = urlNode.item(0).getFirstChild().getNodeValue();
            } else
                imageUrl = null;
        } else
            imageUrl = null;

        return imageUrl;

    } catch (IOException e) {
        return null; // The network probably died, just return null
    } catch (SAXException e) {
        // Problem parsing the XML, log and return nothing
        Log.e("NCRSS", "Error parsing XML", e);
        return null;
    } catch (Exception e) {
        // Anything else was probably another network problem, fail silently
        return null;
    }

}

From source file:nl.mineleni.cbsviewer.jsp.DownloadJSPIntegrationTest.java

/**
 * testcase voor download.jsp./*from  ww  w  .j a  v a  2s .  c  om*/
 * 
 * @throws Exception
 */
@Override
@Test
public void testIfValidResponse() throws Exception {
    response = client.execute(new HttpGet(BASE_TEST_URL + "download.jsp"));
    assertThat("Response status is OK.", response.getStatusLine().getStatusCode(), equalTo(SC_OK));

    boilerplateValidationTests(response);
}

From source file:com.datatorrent.demos.mrmonitor.MRUtil.java

/**
 * This method returns the response content for a given url
 * @param url/*  ww  w .  j a  v  a 2s .c o  m*/
 * @return
 */
public static String getJsonForURL(String url) {
    HttpClient httpclient = new DefaultHttpClient();
    logger.debug(url);
    try {

        HttpGet httpget = new HttpGet(url);

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody;
        try {
            responseBody = httpclient.execute(httpget, responseHandler);

        } catch (ClientProtocolException e) {
            logger.debug(e.getMessage());
            return null;

        } catch (IOException e) {
            logger.debug(e.getMessage());
            return null;
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return null;
        }
        return responseBody.trim();
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:appserver.grupo5.http.java

public static Response Request(Method method, String url, String content, String contentType) {
    Response response;/* w  w  w. j av  a 2s . c  om*/
    try {
        HttpClient client = new DefaultHttpClient();
        HttpUriRequest request;
        switch (method) {
        case GET:
            request = new HttpGet(url);
            break;
        case POST:
            request = new HttpPost(url);
            ((HttpPost) request).setEntity(new StringEntity(content));
            break;
        case PUT:
            request = new HttpPut(url);
            ((HttpPut) request).setEntity(new StringEntity(content));
            break;
        case DELETE:
            request = new HttpDeleteWithBody(url);
            ((HttpDeleteWithBody) request).setEntity(new StringEntity(content));
            break;
        default:
            request = new HttpGet(url);
            break;
        }
        if (method != Method.GET) {
            request.setHeader("Content-type", contentType);
        }
        response = executeRequest(client, request);
    } catch (Exception e) {
        response = Response.status(400).entity(e.getMessage()).build();
    }
    return response;
}

From source file:com.android.dialer.lookup.gebeld.GebeldApi.java

public static ContactInfo reverseLookup(Context context, String number) throws IOException {
    String phoneNumber = number.replace("+31", "0");
    Uri uri = Uri.parse(REVERSE_LOOKUP_URL).buildUpon().appendQueryParameter("queryfield1", phoneNumber)
            .build();/*ww w  . ja v a 2  s.  co  m*/
    // Cut out everything we're not interested in (scripts etc.) to
    // speed up the subsequent matching.
    String output = LookupUtils.firstRegexResult(LookupUtils.httpGet(new HttpGet(uri.toString())),
            "<div class=\"small-12 large-4 columns information\">(.*?)</div>", true);

    String name = null;
    String address = null;

    if (output == null) {
        return null;
    } else {
        Pattern pattern = Pattern.compile(REGEX, 0);
        Matcher m = pattern.matcher(output);

        if (m.find()) {
            name = LookupUtils.fromHtml(m.group(1).trim());

            if (m.find()) {
                address = LookupUtils.fromHtml(m.group(1).trim());

                if (m.find()) {
                    address += "\n" + LookupUtils.fromHtml(m.group(1).trim());
                }
            }
        }
    }

    if (name == null) {
        return null;
    }

    ContactInfo info = new ContactInfo();
    info.name = name;
    info.address = address;
    info.formattedNumber = phoneNumber != null ? phoneNumber : number;
    info.website = uri.toString();

    return info;
}

From source file:com.websqrd.catbot.scraping.HttpHandler.java

public static String executeGet(HttpClient httpclient, String url, StorableInputStream is) throws IOException {
    String type = "text";
    HttpGet httget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httget);
    HttpEntity entity = response.getEntity();
    Header header = entity.getContentType();
    if (header != null) {
        type = header.getValue().toLowerCase();
    }/*from  w  w w  .j a v a2 s . c om*/
    is.addStream(entity.getContent());
    is.rewind();
    return type;
}

From source file:org.xwiki.contrib.repository.pypi.internal.utils.PyPiHttpUtils.java

public static InputStream performGet(URI uri, HttpClientFactory httpClientFactory, HttpContext localContext)
        throws HttpException {
    HttpGet getMethod = new HttpGet(uri);
    CloseableHttpClient httpClient = httpClientFactory.createClient(null, null);
    CloseableHttpResponse response;/*w ww .j  ava 2  s  .  c  om*/
    try {
        if (localContext != null) {
            response = httpClient.execute(getMethod, localContext);
        } else {
            response = httpClient.execute(getMethod);
        }
    } catch (Exception e) {
        throw new HttpException(String.format("Failed to request [%s]", getMethod.getURI()), e);
    }

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == HttpStatus.SC_OK) {
        try {
            return response.getEntity().getContent();
        } catch (IOException e) {
            throw new HttpException(
                    String.format("Failed to parse response body of request [%s]", getMethod.getURI()), e);
        }
    } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
        return null;
    } else {
        throw new HttpException(String.format("Invalid answer [%s] from the server when requesting [%s]",
                response.getStatusLine().getStatusCode(), getMethod.getURI()));
    }
}

From source file:com.codebutler.rsp.Util.java

public static String getURL(URL url, String password) throws Exception {
    Log.i("Util.getURL", url.toString());

    DefaultHttpClient client = new DefaultHttpClient();

    if (password != null && password.length() > 0) {
        UsernamePasswordCredentials creds;
        creds = new UsernamePasswordCredentials("user", password);
        client.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
    }//from   ww  w .j  av  a 2 s.com

    HttpGet method = new HttpGet(url.toURI());
    BasicResponseHandler responseHandler = new BasicResponseHandler();
    return client.execute(method, responseHandler);
}

From source file:nl.mineleni.cbsviewer.jsp.ToegankelijkheidJSPIntegrationTest.java

/**
 * testcase voor download.jsp./*  w  ww  .ja  v a2  s  .  c  o  m*/
 *
 * @throws Exception
 */
@Override
@Test
public void testIfValidResponse() throws Exception {
    this.response = client.execute(new HttpGet(BASE_TEST_URL + "toegankelijkheid"));
    assertThat("Response status is OK.", this.response.getStatusLine().getStatusCode(), equalTo(SC_OK));

    this.boilerplateValidationTests(this.response);
}